Access Qt ui from another class [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I am trying to make a class in Qt that will change things on my MainWindow.
This is my code so far:
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
message_function.h (My Class header)
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMainWindow>
namespace Ui { class MainWindow; }
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp (My Class .cpp)
#include "message_function.h"
void message_function::Print(Ui::MainWindow *ui)
{
ui->label->setText("This is a test");
}
When i buld my project i get this error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall message_function::Print(class Ui::MainWindow *)" (?Print#message_function##QAEXPAVMainWindow#Ui###Z) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QAE#PAVQWidget###Z)
I don't know if i am doing this correctly or if i am doing something wrong.
Can somebody help me get this to work?

Sailordi, the error is in the way you're including the header files.
I did some modifications that should fix it. However I'd like to tell you that what you're doing is not a good programming practice.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "message_function.h"
message_function MeFu;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
MeFu.Print(ui);
}
MainWindow::~MainWindow()
{
delete ui;
}
message_function.h
#ifndef MESSAGE_FUNCTION_H
#define MESSAGE_FUNCTION_H
#include "mainwindow.h"
class message_function
{
public:
void Print(Ui::MainWindow *ui);
};
#endif // MESSAGE_FUNCTION_H
message_function.cpp
#include "message_function.h"
#include "ui_mainwindow.h"
void message_function::Print(Ui::MainWindow* ui)
{
ui->label->setText("This is a test");
}

You shouldn't really be trying to access the MainWindow UI from another class, a better solution is to create a signal in your class that you want to make the change from and a slot in MainWindow that executes the change.

Related

Set connect from another class in QT

I don't get it.
loginview.h
#ifndef LOGINVIEW_H
#define LOGINVIEW_H
#include <QMainWindow>
#include <QDebug>
#include <QPushButton>
class LoginView : public QWidget
{
public:
QWidget *LoginViewSetup(QWidget * wdgMain);
public slots:
virtual void LogInUser();
virtual void CreateUser();
public:
QPushButton *logInBtn;
QPushButton *createBtn;
};
#endif // LOGINVIEW_H
loginview.cpp
#include "loginview.h"
QWidget *LoginView::LoginViewSetup(QWidget * wdgMain)
{
//some code
return wdgCenter;
}
void LoginView::LogInUser()
{
//some code
}
void LoginView::CreateUser()
{
//some code
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <loginview.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QWidget * wdgMain;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
wdgMain = new QWidget(this);
LoginView LoginViewObj;
QWidget *wdgCenter = LoginViewObj.LoginViewSetup(wdgMain);
setCentralWidget( wdgMain );
connect(LoginViewObj.createBtn, SIGNAL (released()), &LoginViewObj, SLOT (CreateUser()));
}
MainWindow::~MainWindow()
{
delete ui;
}
Problem:
QObject::connect: No such slot QWidget::CreateUser() in
../proj/mainwindow.cpp:16
I tried to add Q_OBJECT to the loginview.h and then rebuild project with QMAKE. After that there is no warning about slot, but buttons still not active. Program don't jump to handle of the button in debug mode.
Please, help me to understand what's wrong? I have an object of another class LoginView and I pass this object as a reciever for a slot. Why it passes through my class and searches slot in QWidget?

Cannot link to custom Qt widget

I'm new to Qt and am trying to figure out how to add a custom widget I've created to my main window.
The widget is pretty simple - I just used Qt Designer to create the default widget (I'm calling it Editor) and then added a button to it. Next I included the header of my custom widget in mainwindow.h and added a pointer to it in my MainWindow class. So far, so good. But when I try to create a new instance of Editor, I get the error:
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Editor::Editor(class QWidget *)" (??0Editor##QEAA#PEAVQWidget###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
What do I need to do to be able to link my code?
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "editor.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Editor *editor;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
editor = new Editor(0);
}
MainWindow::~MainWindow()
{
delete ui;
delete editor;
}
editor.h:
#ifndef EDITOR_H
#define EDITOR_H
#include <QWidget>
namespace Ui {
class Editor;
}
class Editor : public QWidget
{
Q_OBJECT
public:
explicit Editor(QWidget *parent = 0);
~Editor();
private:
Ui::Editor *ui;
};
#endif // EDITOR_H
editor.cpp:
#include "editor.h"
#include "ui_editor.h"
Editor::Editor(QWidget *parent) :
QWidget(parent),
ui(new Ui::Editor)
{
ui->setupUi(this);
}
Editor::~Editor()
{
delete ui;
}
CustomWidgetTest.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = CustomWidgetTest
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
editor.cpp
HEADERS += mainwindow.h \
editor.h
FORMS += mainwindow.ui \
editor.ui

undefined-reference Error when defining a function in the *.cpp instead of in the *.h [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I have got a problem: I have 2 classes: mainwindow and ErgebnisAusFortran which look like this:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QString>
#include "ErgbnisAusFortran.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public:
ErgbnisAusFortran Berechnung();
ErgbnisAusFortran Berechnung_1()
{
ErgbnisAusFortran ret;
qDebug() << " ich berechne Berechnung_1..." ;
return ret;
}
private slots:
void on_pb_Calculate_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ErgbnisAusFortran.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pb_Calculate_clicked()
{
ErgbnisAusFortran Ergebnis_1;
ErgbnisAusFortran Ergebnis;
Ergebnis_1 = Berechnung_1();
Ergebnis = Berechnung();
}
ErgbnisAusFortran Berechnung()
{
ErgbnisAusFortran ret;
qDebug() << " ich berechne..." ;
return ret;
}
The thing that puzzles me is the following:
I have 2 methods Berechnung() and Berechnung_1().
Berechnung() is declared in mainwindow.h and defined in mainwindow.cpp
Berechnung_1() is declared in mainwindow.h and defined in mainwindow.h
When I run the program i get the following error concerning Berechnung():
undefined reference to MainWindow::Berechnung().Berechnung_1 works well. This puzzles me because I include mainwindow.h in mainwindow.cpp.
Does anybody know what is wrong?
thank you
itelly
You forgot to qualify the name of the member function:
ErgbnisAusFortran MainWindow::Berechnung()
^^^^^^^^^^^^
so instead this declared a new non-member function, leaving the member function undefined.

Can't instantiate a pointer within main window class

The compiler wont let me instanciate fileprocessor *p; in mainwindow.ccp but I am able to in any other class.
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{
fileprocessor *p;
ui->setupUi(this);
QObject::connect(ui->Open, SIGNAL(clicked()),
this,SLOT(on_action_Open_triggered()));
}
MainWindow::~MainWindow()
{
delete ui;
}
-mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QStandardItemModel>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
I am given fileprocessor:undeclared identifier and p: undeclared identifier
#user4217633: Include fileprocessor.h in the mainwindow.cpp, not in mainwindow.h. There you need a forward declaration:
class fileprocessor; // Just this
You already have such a forward declaration, in fact:
namespace Ui {
class MainWindow;
}

How do I declare objects from my main window in Qt Creator?

This question should hopefully be easy to answer. I created a few buttons in my MainWindow using Qt Creator, and when I go to write the functions for the buttons, the compiler says they were not declared in this scope. What do I need to #include for these objects to be declared? The compiler error for the following would be 'baseDir' was not declared in this scope. baseDir is the objectName for a lineEdit in my window.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainwindow.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void getDir();
void createProj();
private slots:
void on_findDir_clicked();
void on_create_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "functions.h"
#include <QtGui/QApplication>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_findDir_clicked()
{
QString path;
path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null );
baseDir->setText( path );
}
The items you define in your .ui file are not added directly to your main window class, they are added to its ui membre.
Try with:
ui->baseDir->setText( path );
Look at the ui_mainwindow.h file that is generated during the build if you're curious.