Signals and Slots error
-
I tried to write some code after reading the example of Signals and Slots in Qt document. However, when hit run, I encountered a bunch of errors which basically said that "File not found: main.obj". This is the code:
#include <iostream> #include <qobject.h> class A:public QObject { Q_OBJECT public: A():m_value(0) { } int value() const {return m_value;} public slots: void setValue(int value) { if (value!=m_value) { m_value=value; emit valueChanged(value); } } signals: void valueChanged(int newValue){}; private: int m_value; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); A a1,b; QObject::connect(&a1, SIGNAL(valueChanged(int)), &b, SLOT(setValue(int))); a1.setValue(6); std::cout << a1.value() << b.value(); return a.exec(); }
-
@khanhmg
I don't know where exactly problem is, but I changed your code such way and everything works fine)A.h
#pragma once
#include <QtCore\qobject.h>
class A :
public QObject
{
Q_OBJECTpublic:
A();
~A();
int value();
public slots :
void set_value(int value);signals:
void valueChanged(int);
private:
int m_value;
};A.cpp
#include "A.h"A::A()
{
m_value = 0;
}A::~A()
{
}void A::set_value(int value)
{
if (value != m_value)
{
m_value = value;
emit valueChanged(m_value);
}
}int A::value()
{
return m_value;
}main.cpp
#include <QtCore\qobject.h>
#include <QtCore\QDebug>
#include "A.h"
#include <QtCore\qcoreapplication.h>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);A *a1 = new A; A *b = new A; a1->set_value(6); QObject::connect(a1, SIGNAL(valueChanged(int)), b, SLOT(set_value(int))); qDebug() << a1->value() << b->value(); return a.exec();
}
-
Now I'm confused too, what's wrong with this code? Why slot is not called??
-
@khanhmg
Hah, so simple. Correct main:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);A a1, b; QObject::connect(&a1, SIGNAL(valueChanged(int)), &b, SLOT(set_value(int))); a1.set_value(6); qDebug() << a1.value() << b.value(); return a.exec();
}
In my first variant I put QObject::connect(...); after calling set_value function
-
@khanhmg
And a1 and b objects are underlined just because you created them but not has used, if you mean that (I'm not sure is this sentence correct, if not - sorry for my English)). If you add, for example, a1.set_value(5) - a1 will not be underlined anymore -
It appears to be a linker error... can you post your .pro file (or is that now sloved?)
You have to add the path as well as the file to the .pro file - in case you have not done this :o -
@code_fodder
I think my .pro file will not help you, because I didn't change anything there, I just created new Qt Console Application, there - a new class and copied code, that I posted, that's all.
Just in case here is the .pro file:QT += core
QT -= guiTARGET = fddcgh //name of a project,
CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp
a.cppHEADERS +=
a.h -
@AntonZelenin Is that .pro file copied + pasted in exactly? if it is then there is a little issue there. When you add source or header file on new lines you need to use the "line continuation" character, which is the backslash "\" at the end of each line. Just check that you have that : )
i.e. like this:
SOURCES += main.cpp \ a.cpp HEADERS += \ a.h