GBayo1
22nd March 2010, 13:12
Hi all,
I was wondering would someone be able to help me with a slight problem. I am trying to use signals and slots within my program. From QT, we know that “all classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObjectâ€. That’s fine. So the following is an example I’m trying to implement, taken directly from http://doc.trolltech.com/4.6/signalsandslots.html
Counter.h
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
Counter.cpp
#include “Counter.hâ€
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
Then when trying to instantiate the class:
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
I get the following linking errors:
error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Counter::metaObject(void)const " (?metaObject@Counter@@UBEPBUQMetaObject@@XZ)
error LNK2001: unresolved external symbol "public: virtual void * __thiscall Counter::qt_metacast(char const *)" (?qt_metacast@Counter@@UAEPAXPBD@Z)
error LNK2001: unresolved external symbol "public: virtual int __thiscall Counter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Counter@@UAEHW4Call@QMetaObject@@HPA PAX@Z)
error LNK2001: unresolved external symbol "protected: void __thiscall Counter::valueChanged(int)" (?valueChanged@Counter@@IAEXH@Z)
From looking at forums (http://lists.trolltech.com/qt-interest/2005-12/msg00374.html), it seems that we need to moc the source – “You need to call "moc" on your Counter header file and make sure the compiler includes the output of moc into the binary. How you'd do that depends on the compiler being used and if you use custom Makefiles, qmake-projects or something else“.
As far as I can see, I have a moc file and it is being updated. Would someone please help me with this probelm and let us know how to overcome it.
Thank you for your time
I was wondering would someone be able to help me with a slight problem. I am trying to use signals and slots within my program. From QT, we know that “all classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObjectâ€. That’s fine. So the following is an example I’m trying to implement, taken directly from http://doc.trolltech.com/4.6/signalsandslots.html
Counter.h
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
Counter.cpp
#include “Counter.hâ€
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
Then when trying to instantiate the class:
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
I get the following linking errors:
error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Counter::metaObject(void)const " (?metaObject@Counter@@UBEPBUQMetaObject@@XZ)
error LNK2001: unresolved external symbol "public: virtual void * __thiscall Counter::qt_metacast(char const *)" (?qt_metacast@Counter@@UAEPAXPBD@Z)
error LNK2001: unresolved external symbol "public: virtual int __thiscall Counter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Counter@@UAEHW4Call@QMetaObject@@HPA PAX@Z)
error LNK2001: unresolved external symbol "protected: void __thiscall Counter::valueChanged(int)" (?valueChanged@Counter@@IAEXH@Z)
From looking at forums (http://lists.trolltech.com/qt-interest/2005-12/msg00374.html), it seems that we need to moc the source – “You need to call "moc" on your Counter header file and make sure the compiler includes the output of moc into the binary. How you'd do that depends on the compiler being used and if you use custom Makefiles, qmake-projects or something else“.
As far as I can see, I have a moc file and it is being updated. Would someone please help me with this probelm and let us know how to overcome it.
Thank you for your time