How to Use Classes derived from QObject to send signals and have slots
-
Hi
I have been trying to have a class called Tool that derives from QObject and declares several slots.
I my QMainWindow class (TheClass), I am trying to use that class.
For instance TheClass has a private member:
@private: Tool* TheTool;@
Now when it comes to connect to signals from Tool, I do in the constructor of TheClass:
@connect(TheTool,SIGNAL(theToolSignal),this,SLOT(myToolSlot()));@
Which compiles fine, but when I run it, I get the message
bq. QObject::connect: Cannot connect (nulll)::theToolSignal to TheClass::myToolSlot()
Which seems to indicate that TheTool is not instantiated.
Yet, when I make a call to a member function
@TheTool->member()@
I see from its qDebug() output that it is being called
I have read the notes on the QObject class and that it has a Q_DISABLE_COPY macro which means that implied or explicit assignments won't work and that one cannot instantiate the QObject?
My question for you is then:
How can I modify my code so that I can have a class derived from Q_Object that uses Signals and Slots, and then use that class from another class?
-
Hi
Yes, I transcribed the signal wrong in this question.
However, the problem I have is that when I do use new, the linker starts whinging at me:
bq. in function
Tool::Tool()': Tool.h:77: undefined reference to
vtable for Tool::Tool' -
Hi
I finally figured it out, your mentioning the Q_OBJECT macro again made me check and lo, I did not include the "tool.moc" file.
Interestingly enough, it is not created by my cmake either even though qt4_automoc is fed both the tool as well as the MyClass
I did a moc ../Tool.h > tool.moc in my build directory after appending the include "tool.moc" line in my tool.cpp file, then make and voila! it is compiling!
Thank you, you're a star :)
-
FYI; getting the cmake to build the moc for the tool as wel was as simple as adding a line
@set(myCoolProject_H tool.h myclass.h)@
In addition to having these lines:
@QT4_WRAP_UI(myCoolProject_H ${myCoolProject_GUI} )
qt4_automoc(${myCoolProject_SRCS})@