Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions
What is Q_OBJECT macro and what is the need of it?
pabitra.das last edited by
As I am new to Qt can someone please explain me what is Q_OBJECT macro and when we need it ?
Binary91 last edited by
Hello and welcome to devnet!
You use the Q_OBJECT macro to tell the compiler that this class uses own signals and slots.
AFAIK Qt doesn't use standard signal & slot concept and with that macro you need to run meta object compiler (moc) first --> it changes the code in "real" c++ code that is usefull for the g++ compiler.
Hence, you write it in the classes you define to have own signals & slots and then you pre-compile it with moc and finally compile both (moc_file.cpp & file.cpp)... (I hope that is correct like this^^)
TheBadger last edited by
I hate to be the one to tell you to read the documentation...
The documentation on Qt is very good, for that question look at "QObject":http://qt-project.org/doc/qt-5/qobject.html
So to answer "when we need it", in there you get the following:
Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.
And to expand that a bit, for everything that inherits from QObject :)