Results 1 to 10 of 10

Thread: why we need Q_OBJECT???

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default why we need Q_OBJECT???

    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.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    did you read this?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    Quote Originally Posted by spirit View Post
    did you read this?
    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.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    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:

    Qt Code:
    1. /* tmake ignore Q_OBJECT */
    2. #define Q_OBJECT \
    3. public: \
    4. Q_OBJECT_CHECK \
    5. static const QMetaObject staticMetaObject; \
    6. virtual const QMetaObject *metaObject() const; \
    7. virtual void *qt_metacast(const char *); \
    8. QT_TR_FUNCTIONS \
    9. virtual int qt_metacall(QMetaObject::Call, int, void **); \
    10. private:
    11. /* tmake ignore Q_OBJECT */
    12. #define Q_OBJECT_FAKE Q_OBJECT
    13. /* tmake ignore Q_GADGET */
    14. #define Q_GADGET \
    15. public: \
    16. static const QMetaObject staticMetaObject; \
    17. private:
    18. #else // Q_MOC_RUN
    19. #define slots slots
    20. #define signals signals
    21. #define Q_SLOTS Q_SLOTS
    22. #define Q_SIGNALS Q_SIGNALS
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    Quote Originally Posted by phillip_Qt View Post
    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 ...
    Last edited by wagmare; 26th March 2009 at 06:04.

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    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

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    Quote Originally Posted by phillip_Qt View Post
    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    Quote Originally Posted by talk2amulya View Post
    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 ...

  9. #9
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: why we need Q_OBJECT???

    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: why we need Q_OBJECT???

    Quote Originally Posted by wagmare View Post
    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.

    Quote Originally Posted by talk2amulya View Post
    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.

    Quote Originally Posted by wagmare View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    talk2amulya (26th March 2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.