Results 1 to 4 of 4

Thread: Q_SIGNALS vs signals and public Q_SLOTS: vs public slots in class definition

  1. #1
    Join Date
    Feb 2015
    Location
    Poland
    Posts
    34
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Q_SIGNALS vs signals and public Q_SLOTS: vs public slots in class definition

    Hi
    I am wonder why I encountered these macros (Q_SINGALS and Q_SLOTS) in Qt Remote Objects examples (maybe they appears earlier in other places). From my point of view "signals" and "public slots" work perfectly. But I realize my field of experience is limited. So I am wonder where these macros would help.
    I use F2 (in Qt Createor) to investigate macro code, but no luck, I push F1 to show documentation, no luck, I googled various phrases to investigate meaning of these macors, but no luck.
    So maybe some one experienced (or just up to date with this information) can explain to me where Q_SINGALS and Q_SLOTS macros can be useful?!?

    thanks and best regards
    Szyk Cech


    Added after 18 minutes:


    Oh! The same question is for Q_EMIT macro vs emit clause...
    Last edited by Szyk; 4th February 2018 at 16:51.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Q_SIGNALS vs signals and public Q_SLOTS: vs public slots in class definition

    As far as I can tell, the difference is just a syntactical sugar.
    The macros are the actual c++ definitions the moc uses to generate the moced classes.
    The keywords such 'signal' 'slot' or 'emit' are there to make it more like a feature, like a c++ keyword when typing the code.
    You can use the macros instead they do exactly the same.
    In fact, I use the macros not the keywords in my code.

    This is my personal take on it, I might be wrong.

    I am not sure why QtCreator didn't open the macros with F2 for you.
    For me it works. (qobjectdefs.h)
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    552
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_SIGNALS vs signals and public Q_SLOTS: vs public slots in class definition

    'signals' and 'slots' are no keywords - they are just defines to Q_SIGNALS and Q_SLOTS - see qobjectdefs.h ~line 90:
    Qt Code:
    1. # define slots Q_SLOTS
    2. # define signals Q_SIGNALS
    To copy to clipboard, switch view to plain text mode 
    emit / Q_EMIT is just an empty define - it's just there for readability.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,023
    Thanks
    280
    Thanked 819 Times in 808 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Q_SIGNALS vs signals and public Q_SLOTS: vs public slots in class definition

    'signals' and 'slots' are no keywords
    Correct in a C++ sense, but they -are- important and are treated as keywords by MOC, which will use them to generate the actual C++ code that defines the signal methods in the moc_*.cpp files. They probably also play a role in creating automatic connections between widgets defined in the UI file and slots in your C++ code (for example, a QPushButton named "myButton" defined in a UI file and a slot declared as "on_myButton_clicked" in the .h file for the UI). And if you use the old-style connect() with the "SIGNAL()" and "SLOT()" convention, not using the "signal" and "slot" keywords / macros will result in runtime errors because the Qt runtime will be unable to make the connections.

    The new Qt connect() versions make it more obvious that signals and slots are just ordinary functions (even lambdas will work as slots). And even though "emit" is a no-op, it's a way of indicating to whoever inherits your code for maintenance (even a future version of yourself) that this is no ordinary function call.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 22nd February 2011, 07:55
  2. Public functions in a QThread class: safe?
    By dmginc in forum Qt Programming
    Replies: 6
    Last Post: 31st January 2011, 10:33
  3. private and public slots ??
    By salmanmanekia in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2008, 11:49
  4. default on class' public data
    By baray98 in forum General Programming
    Replies: 2
    Last Post: 29th July 2008, 02:04
  5. Replies: 5
    Last Post: 20th April 2008, 20:30

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.