[Solved] Problem with get method
-
Hello,
I have a problem with the use of the get method, I think the synthax of something is wrong but I don't know where.. Would anyone see what is wrong?
I write here the important lines
ui_mainform.h:
@class Ui_MainForm
{
public:
QTextEdit *errorBrowser;void setupUi(QMainWindow *MainForm)
{
errorBrowser = new QTextEdit(centralwidget);
errorBrowser->setObjectName(QStringLiteral("errorBrowser"));
errorBrowser->setGeometry(QRect(500, 490, 491, 121));
errorBrowser->setReadOnly(true);
}
};@mainform.h:
@class MainForm : public QMainWindow,
private Ui::MainForm // Die Klasse MainForm wird in der automatisch erzeugten Header-Datei ui_mainform.h automatisch erstellt
{
Q_OBJECTpublic:
explicit MainForm(QWidget parent = 0);
MainForm::~MainForm();
QTextEdit getBrowser() const;private:
Ui::MainForm *ui;
};@mainform.cpp:
@MainForm::MainForm(QWidget *parent)
: QMainWindow(parent)
{
ui->setupUi(this);camXPos = "ERROR!";
camXPos = "ERROR!";
camZPos = "ERROR!";}
MainForm::~MainForm()
{
delete ui;
}QTextEdit* MainForm::getBrowser() const
{
return ui->errorBrowser;
}@errorMsg.h:
@class errorMsg : public QObject
{
Q_OBJECTpublic:
errorMsg();
~errorMsg();QTextEdit *browser;
};@errorMsg.cpp:
@errorMsg::errorMsg()
{
browser = MainForm::getBrowser();
}errorMsg::~errorMsg()
{}@The problem comes with the constructor of errorMsg, the error message says
@errorMsg.cpp(8): error C2352: 'MainForm::getBrowser' : illegal call of non-static member function
copy2\linsenmessplatz3\mainform.h(35) : see declaration of 'MainForm::getBrowser'@Thank you in advance for your advises!
-
You probably need to remove "const" from getBrowser().
-
Ah, sorry, I have missed that line:
@
browser = MainForm::getBrowser();
@This is obviously wrong. You need to either make MainWindow into a Singleton, or change the architecture of your project (I advise the latter).
-
You cannot invoke getBrowser() without an object (MainWindow instance). So, you need to restructure your code so that you can pass the pointer to that object into your errorMsg class.
This is also not an ideal solution (why on Earth should error handling class be aware that a GUI exists?), but it will at least work.
Yet another option would be to sent a signal from errorMsg to MainWindow, and handle the text changing there (that solution seems way better).
-
-
Correct.
@
connect(line, SIGNAL(signalChangeText(QString)), ui->errorBrowser, SLOT(changeText(QString)));
@ -
Ok, but my @ui is undefined@
It wasn't defined when I took the project, so that I had to define it manually than a new Widget project with Qt Creator, but something is probably wrong. I defined it in the Mainform.h that I shown at the beginning of the topic.
The Mainform produces a ui_MainForm file too, which defines the QWidgets and theirs proprieties. I don't know if I can create both *ui and ui_Mainform..
-
I made some changes, and now it seems to be a little better, but there are some new errors. I had to define Ui::MainForm *ui; in both mainform.h and errorMsg.h to avoid the old errors with "undefined ui", even if I have no idea if it works well..
The errors are now:
@errorMsg.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall errorMsg::metaObject(void)const " (?metaObject@errorMsg@@UBEPBUQMetaObject@@XZ)
errorMsg.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall errorMsg::qt_metacast(char const *)" (?qt_metacast@errorMsg@@UAEPAXPBD@Z)
errorMsg.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall errorMsg::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@errorMsg@@UAEHW4Call@QMetaObject@@HPAPAX@
And my new MainForm.h
@#include <QMainWindow>
namespace Ui {
class MainForm;
}class MainForm : public QMainWindow,
private Ui::MainForm // ui_mainform.h automatic creation
{
Q_OBJECTpublic:
explicit MainForm(QWidget *parent = 0);
~MainForm();private:
Ui::MainForm *ui;
};@