Results 1 to 5 of 5

Thread: Signal/Slot methods

  1. #1
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Signal/Slot methods

    Hello,

    I'm new to C++ and Qt. I was wondering if the methods used to connect signals with slots actually get called/executed? (I realize the slot gets called, otherwise it wouldn't be too useful ) For example...

    Qt Code:
    1. connect( this, SIGNAL( signalMethod( int signalArg ) ), someObject, SLOT( slotMethod( int slotArg ) ) );
    To copy to clipboard, switch view to plain text mode 

    when I emit the signal with...

    Qt Code:
    1. emit signalMethod( someInt );
    To copy to clipboard, switch view to plain text mode 

    does the method "signalMethod()" get called with the argument "someInt" (if it is defined in the class) before the signal is delivered or is it just a placeholder of some sort for the moc?

    Put another way if the signalMethod has code associated with it in the class definition, does that determine if it gets called or not?

    Thanks for any insight.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal/Slot methods

    Yes, the meta object compiler adds a few methods to a class that implements signals/slots.
    A method is added for every signal that you define in your class, with the same signature.

    For example, for your signal example, in the source generated by moc you can find a method named signalMethod(int).

    When you emit a signal( with emit), you actually call that auto-generated method.
    This, in turn, calls QMetaObject::activate.
    This checks to see if actually something is connected to this signal(a slot).
    Further, the connection list of the object is investigated to find the destination object. Also, here is checked what kind of connection we have ( direct, queued ), plus a lot of other verifications.
    Finally, qt_metacall is called, that actually calls the corresponding slot in the target object.

    This is the big picture.

    You could find out more by debugging and looking over what moc generates.

    Regards

  3. #3
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Signal/Slot methods

    Thank you for that insight Marcel. It saved me a lot of digging.

    I take it from your explanation of how it works, that it is not possible to have user specified code as a signal method. As in...

    Qt Code:
    1. class fiddlestix {
    2. Q_OBJECT
    3.  
    4. public signals:
    5. void doThisWhenISignal( int foo );
    6.  
    7. };
    8.  
    9. fiddlestix::doThisWhenISignal( int foo )
    10. {
    11.  
    12. foo += 53;
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    In other words, I cannot define a section of code that I wish to be executed every time I emit the signal that does something _before_ Qt starts operating on it?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signal/Slot methods

    No, you cannot do that.
    At most you can connect another slot to that signal, but before you connect the actual slot.
    The first slot will get executed before the second. This is if you want to do something before the second slot is executed.
    The connect order dictates the slot execution order for direct connections.

    If you want to do something before actually the signal is emitted, you have only one chance: if you emit the signal yourself, i.e. by calling (emit signal). Then you could put some code before the emit statement.

    EDIT: when declaring signals, you must not give them a visibility modifier( public, protected, private ). Just signals:. This is opposed to slots, where you always have to specify their visibility.

    Regards

  5. #5
    Join Date
    Jun 2007
    Posts
    56
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Signal/Slot methods

    Got it. Thanks Marcel!

    Also I take your point re: visibility of signals vs. slots. Good insight.

Similar Threads

  1. How to access OCX methods and events
    By joy in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2018, 17:37
  2. What argument types for Signal/Slot?
    By DPinLV in forum Qt Programming
    Replies: 5
    Last Post: 2nd August 2006, 20:08
  3. What if not Signal/Slot
    By munna in forum General Discussion
    Replies: 2
    Last Post: 26th May 2006, 07:48
  4. Replies: 4
    Last Post: 23rd January 2006, 17:51
  5. What's the relationship between signal/slot and event?
    By twosnowman in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 18:13

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.