Unresolved external Symbol formInterface::forminterface => Constructor
-
Hi there,
i guess i´m doing steps Forward by taking the calculator example as a reference.
I´m trying to Change a variable in a class with the spinbox of a widget
I made-
a simple widget with only one spinbox and called it "Form" with the creator
-
made a class "formInterface" with a SLOT to set an internal Variable
-
connect Form´s SIGNAL valueChanged and formInterface´s SLOT SetVal();
-
checked if everything is listed in the .pro File
As the Headline says i´m getting the error
unresolved external Symbol formInterface::forminterface
which seems to me to be the constructor. I don´t understand what is missing.best regards
@#ifndef MAIN_H
#define MAIN_H
#include <QApplication>
#include "form.h"
#include "forminterface.h"
#include <iostream>class main
{
public:
main();
};#endif // MAIN_H
@
@#include "main.h"
using namespace std;void main(int argc, char *argv[])
{QApplication app(argc, argv); Form *test = new Form; formInterface *FI = new formInterface; QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int))); test->show(); cout << "TEST\n"; app.exec();
}
@
@#ifndef FORM_H
#define FORM_H#include <QWidget>
namespace Ui {
class Form;
}class Form : public QWidget
{
Q_OBJECTpublic:
explicit Form(QWidget *parent = 0);
~Form();private:
Ui::Form *ui;
};#endif // FORM_H
@
@#include "form.h"
#include "ui_form.h"Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}Form::~Form()
{
delete ui;
}
@
@#ifndef FORMINTERFACE_H
#define FORMINTERFACE_Hclass formInterface : public QObject
{
Q_OBJECT
public:
formInterface();
public slots :
void updateVal(int i);private:
int val;
};
#endif // FORMINTERFACE_H
@
@#include "forminterface.h"
#include <iostream>formInterface::formInterface()
{
}void formInterface::updateVal(int i)
{
val = i;
std::cout << i << "\n";
}
@
@FORMS +=
form.uiHEADERS +=
form.h
main.h
forminterface.hSOURCES +=
form.cpp
main.cpp
forminterface.cpp
QT += core gui widgets
CONFIG += qt
@ -
-
You need to initialise the base class (QObject) in your formInterface class, like this:
@
formInterface::formInterface() : QObject()
@Or, even better, do it the Qt way and include parent info in the constructor:
@
// H
public:
formInterface(QObject *parent = 0);// CPP
formInterface::formInterface(QObject *parent) : QObject(parent)
@ -
Hey hey,
i also included <QObject> to formInterface.h and tryed both variants, but still getting the error massage.
-
If you are pasting the error message here exactly as it is printed, then the matter is simple: you have a typo in the constructor name (forminterface instead of formInterface).
-
Mh... i set up a new Project and copyed everything from the first one and then it was ok.
Since i want to Change a variable inside formInterface.cpp i gave it a Slot "updateValue(int) and gave the Form.h a Signal valueChanged(int)
and connected them in main
QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int));
The updateValue function is
@
void formInterface::updateVal(int i)
{
val = i;
std::cout << val << "\n";
}
@
so, everytime the function is triggered i should get an Output, what doesn´t happen. But during compiling i don´t get warnings, that Connection failed.
Also a cout command in the constructor Comes to Action, when i cancel the window when running...
Complete Code beneathmain:
@#include "main.h"
using namespace std;void main(int argc, char *argv[])
{QApplication app(argc, argv); Form *test = new Form; formInterface *FI = new formInterface; QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int))); test->show(); cout << "TEST\n"; app.exec();
}
@
Form.h
@#ifndef FORM_H
#define FORM_H#include <QWidget>
namespace Ui {
class Form;
}class Form : public QWidget
{
Q_OBJECTpublic:
explicit Form(QWidget *parent = 0);
~Form();private:
Ui::Form *ui;
signals:
void valueChanged(int);
};#endif // FORM_H
@
Form.cpp
@#include "form.h"
#include "ui_form.h"Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
}Form::~Form()
{
delete ui;
}
@
forminterface.h
@#ifndef FORMINTERFACE_H
#define FORMINTERFACE_H
#include <QObject>
class formInterface : public QObject
{
Q_OBJECT
public:
formInterface();
public slots :
void updateVal(int i);private:
int val;
};
#endif // FORMINTERFACE_H
@
formInterface,cpp
@#include "forminterface.h"
#include <iostream>formInterface::formInterface(): QObject()
{
std::cout << "formInterface Konstruktor "<< val << "\n";
}void formInterface::updateVal(int i)
{
val = i;
std::cout << val << "\n";
}
@ -
Connections are created at runtime. Check out the console output when the application is running (compiled in debug mode!).
Just to be sure, please use qDebug() instead of std::cout.
In order to updateVal() to be triggered, you need to emit the valueChanged() signal somewhere in Form class. I don't see any place where you do that.
-
At first I tryed to have a member function wich Points to the valueChanged() of the ui
@
void Form::valueChanged(int i)
{
ui->spinBox->valueChanged(i);
}@But when i´m commenting this code in, i get the error,
moc_form.obj:-1: error: LNK2005: "public: void __thiscall Form::valueChanged(int)" (?valueChanged@Form@@QAEXH@Z) is allready defined in form.obj
and plus i have a book which says that code from Signal methods is written by the MOC
-
You have to specify a place where this signal will be emitted. The rest of the magic is done by MOC, indeed. But you need to tell it when to run. This is done by using the emit keyword (not really, but don't worry about that right now).
You should not define a function that has the same name as your signal! The code you posted in your last post is wrong, and that is precisely why you are not getting any output. The spinbox will emit the signal for you, all you need to do is to connect it to your slot. You do not need to define a signal in your Form class.
-
Originally i didn´t had that Signal method in the form.cpp
So i guess this
@
QObject::connect(test, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int)));
@
is wrong and to connect the spinBoxe´s valueChanged somehow i Need to reference the spinbox via the form variable?
I tryed like this test->ui->... but originally the ui is a private pointer. -
Something like this would work, but would require a bit of refactoring:
@
connect(ui->spinBox, SIGNAL(valueChanged(int)), formInterfacePointer, SLOT(updateVal(int)));
@Don't worry, I know signals and slots can be confusing in the beginning, but knowing them is well worth the effort.
You could also create a signal chain and thus expose the spinbox's signal through your Form class, but that's a lesson for later.
-
I also tryed this
@
QObject::connect(test->ui->spinBox, SIGNAL(valueChanged(int)), FI, SLOT(updateVal(int)));
@
but getting the warning, "Usagae of undefined Type Ui::Form" and i should take a look at the declaration. wich is done in Form.h
and that "left of ->spinBox must be a pointer to a Class/Struct/Uonion/generic Type"@
#ifndef FORM_H
#define FORM_H#include <QWidget>
namespace Ui {
class Form;
}class Form : public QWidget
{
Q_OBJECTpublic:
explicit Form(QWidget *parent = 0);
~Form();
Ui::Form *ui;
private:signals:
void valueChanged(int);};@
-
[quote author="Flaming Moe" date="1409809494"]but getting the warning, "Usagae of undefined Type Ui::Form" and i should take a look at the declaration. wich is done in Form.h
and that "left of ->spinBox must be a pointer to a Class/Struct/Uonion/generic Type"[/quote]That is because Ui class is not visible from your main() routine. As said, it does take a bit of getting used to.
-
Ok, i think i got it.
I throwed out the formInterface.h/.cpp and do everything in form.h/.cpp
and connect the Signal and the Slot in the constructor.Is the qt Application blocking the outputstream via cout? I get the Output when i Close the app´s window.
main:
@#include "main.h"
using namespace std;void main(int argc, char *argv[])
{
QApplication app(argc, argv);
Form *test = new Form;
//formInterface *FI = new formInterface;
test->show();
cout << "TEST\n";
app.exec();
}@
form.h:
@#ifndef FORM_H
#define FORM_H
#include <iostream>
#include <QWidget>namespace Ui {
class Form;
}class Form : public QWidget
{
Q_OBJECTpublic:
explicit Form(QWidget *parent = 0);
~Form();
Ui::Form *ui;public slots :
void updateVal(int i);signals:
void valueChanged(int);private:
QWidget window;
int val;
};#endif // FORM_H
@
form.cpp:
@#include "form.h"
#include "ui_form.h"Form::Form(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form)
{
ui->setupUi(this);
QObject::connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(updateVal(int)));
window.setWindowTitle("Hallo Qt");
window.setGeometry(30,30,200,200);
}void Form::updateVal(int i)
{
val = i;
if(val == 5) window.show();
if(val == 7) window.close();
std::cout << val << "\n";
}Form::~Form()
{
delete ui;
}
@ -
Yes, it can happen. Better use qDebug().