PDA

View Full Version : why we need Q_OBJECT???



phillip_Qt
25th March 2009, 11:56
Hi All
I've a doubt.
If we wants to define a slot then we've to use Q_OBJECT macro in my private part.
I need to know what Q_OBJECT do internally?

thank you all.

spirit
25th March 2009, 12:06
did you read this (http://doc.trolltech.com/4.5/qobject.html#Q_OBJECT)?

phillip_Qt
26th March 2009, 04:15
did you read this (http://doc.trolltech.com/4.5/qobject.html#Q_OBJECT)?

Hi
thanks for reply. Actually before posting this question i wnet through the Assistant and ched that page. But i c'd not found how it helps a function to work as a slot.

Thank you.

talk2amulya
26th March 2009, 05:59
Q_OBJECT is Qt's way to declare that a particular class's object wants to do some object communication. what a Q_OBJECT entry in a class does is that it tells the compiler to create moc files for that particular class which will have desired entries for signals and slots and other metatype information. If you are more curious to know about it, u should see qobjectdefs.h in Qt folder. Here is how Q_OBJECT is defined:


/* tmake ignore Q_OBJECT */
#define Q_OBJECT \
public: \
Q_OBJECT_CHECK \
static const QMetaObject staticMetaObject; \
virtual const QMetaObject *metaObject() const; \
virtual void *qt_metacast(const char *); \
QT_TR_FUNCTIONS \
virtual int qt_metacall(QMetaObject::Call, int, void **); \
private:
/* tmake ignore Q_OBJECT */
#define Q_OBJECT_FAKE Q_OBJECT
/* tmake ignore Q_GADGET */
#define Q_GADGET \
public: \
static const QMetaObject staticMetaObject; \
private:
#else // Q_MOC_RUN
#define slots slots
#define signals signals
#define Q_SLOTS Q_SLOTS
#define Q_SIGNALS Q_SIGNALS

wagmare
26th March 2009, 05:59
Hi All
I've a doubt.
If we wants to define a slot then we've to use Q_OBJECT macro in my private part.
I need to know what Q_OBJECT do internally?

thank you all.
also another thing ...
c++ compiler will compile SIGNAL and SLOT as a string and not as a function .. Q_OBJECT's moc only does the magic of linking unrelated two objects ... for that only we get warning at run time of the qt application ... also to use qt resources , designer plugins, converting design.ui to ui_design.h ....etc , without Q_OBJECT nothing is possible ..

signal -slot connection are established in run time and signal and slot signatures are jusit string , Q_OBJECT::connect() will connect this two things ...

talk2amulya
26th March 2009, 06:10
no, SIGNAL and SLOT are not compiled as string at all..they are actually just internally treated as "protected"..when u include QObject, it internally includes qobjectdefs.h which has all the definitions of SIGNALS, SLOTS, Q_OBJECT, Q_SIGNALS, Q_SLOTS, what other functions are included in the moc files..everything

spirit
26th March 2009, 06:19
Hi
thanks for reply. Actually before posting this question i wnet through the Assistant and ched that page. But i c'd not found how it helps a function to work as a slot.

Thank you.

from Qt Assistan


...that declares its own signals and slots or that uses other services provided by Qt's meta-object system.

and also read more about "Meta-Object System", investigating sources is helpful too. ;)

wagmare
26th March 2009, 06:29
no, SIGNAL and SLOT are not compiled as string at all..they are actually just internally treated as "protected"..when u include QObject, it internally includes qobjectdefs.h which has all the definitions of SIGNALS, SLOTS, Q_OBJECT, Q_SIGNALS, Q_SLOTS, what other functions are included in the moc files..everything

thanks ... why i am saying that SIGNAL and SLOT were considered as string is ..

what an expert told to me when i ask her
"why the connect is not showing error in compiling but only in run time in console ..?"
the reply she gave ..
"signal-slot connection are established at run-time , not at compiler time"

"Signal-Slot signatures are just strings for compiler thus no error it showing"

"QObject::Connect() will output a detailed warning when a connection failed"


this is the reply ... so only i suggest this to the latter post ...

talk2amulya
26th March 2009, 06:34
well, its true that signal slot connection is established at run-time and yes, it will output a warning if connection fails..but its not just string but a "protected" access specifier..maybe she says its a string cuz its actually #define'd

wysota
26th March 2009, 07:49
c++ compiler will compile SIGNAL and SLOT as a string and not as a function ..
Have you tried manually calling a slot as a function or retrieving its address? I assure you it works. Slots (and signals too) are regular functions, just with some extra treatment from moc.


Q_OBJECT's moc only does the magic of linking unrelated two objects
Actually it does much more. Open a file generated by moc and see for yourself.


... for that only we get warning at run time of the qt application ... also to use qt resources , designer plugins, converting design.ui to ui_design.h ....etc , without Q_OBJECT nothing is possible ..
That's not true. You don't need Q_OBJECT to use Qt resources and you don't need the macro to "convert design.ui to ui_design.h" (nor to use it).


signal -slot connection are established in run time and signal and slot signatures are jusit string
Yes, this is true with one exception. They are not "just" strings, they are regular methods as well. The slot method is implemented by the programmer and the signal method is implemented by moc.


they are actually just internally treated as "protected"..
No, that's not true. Signals are treated as protected. Slots are treated based on how you defined them - if you defined them as private, they are private when called as a function and public when defined as public. The situation changes when you perform a connection - a slot called as a slot is always public, regardless of the section it was declared in.


thanks ... why i am saying that SIGNAL and SLOT were considered as string is ..

what an expert told to me when i ask her
"why the connect is not showing error in compiling but only in run time in console ..?"
the reply she gave ..
"signal-slot connection are established at run-time , not at compiler time"

"Signal-Slot signatures are just strings for compiler thus no error it showing"

"QObject::Connect() will output a detailed warning when a connection failed"


this is the reply ... so only i suggest this to the latter post ...

Signatures are strings, that's correct. Moreover a signal signature is prepended with "1" and a slot signature is prepended with "2" so that you can't connect a slot to a slot. But that doesn't change the fact both the slots and the signals are regular methods of the class they are declared in.