Results 1 to 7 of 7

Thread: Signals and Slots

  1. #1
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Signals and Slots

    Hello,

    in my app I have a custom class for networking that uses a QNetworkAccessManager object inside it.
    From the QNetworkAccessManager's requestFinished(..) slot I emit some custom signal that carries some data
    and which I want to handle in other classes.

    My problem is following. Imagine in some class B, I tie a slot to the signal I mentioned above.
    Then I tie another slot to this signal. In the end we may have that to my signal (customSignal) there are
    10 slots connected in some other class B. I don't want that once the signal is emitted all these
    10 slots get called simultaneously. Rather at some point in time, I want the signal to call
    only one slot, at another point in time, only another slot and etc.

    How can I achieve this?

    I know I could for example use different my custom network objects (but I was told it is not recommended??).
    e.g., like this:

    Qt Code:
    1. MyNetworkClass *network1 = new MyNetworkClass();
    2. bool res = QObject::connect(network1, SIGNAL(signalSuccess(QVariant)), this, SLOT(CustomSLot1(QVariant)));
    3.  
    4. MyNetworkClass *network2 = new MyNetworkClass();
    5. bool res = QObject::connect(network2, SIGNAL(signalSuccess(QVariant)), this, SLOT(CustomSLot2(QVariant)));
    6.  
    7. MyNetworkClass *network3 = new MyNetworkClass();
    8. bool res = QObject::connect(network3, SIGNAL(signalSuccess(QVariant)), this, SLOT(CustomSLot3(QVariant)));
    To copy to clipboard, switch view to plain text mode 

    I think it would work this way also??? Thanks.
    Last edited by ggdev001; 20th February 2013 at 08:06.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Signals and Slots

    Instead have one network object with different signals

    signalSuccess1(QVariant)
    signalSuccess2(QVariant)
    signalSuccess3(QVariant)
    ..
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    ggdev001 (20th February 2013)

  4. #3
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots

    If you're trying to balance load between threads, I suggest you used producer-consumer pattern(wiki).

    Or you can implement custom recipient object, that will call only one slot, somewhat along the lines:
    Qt Code:
    1. class CustomRecipient {
    2. public slots:
    3. void CallOneSlot (QVariant var) {
    4. // or any other marshalling scheme
    5. current_callee = (++current_callee) % callee.size ();
    6. QMetaObject::invokeMethod (callee[current_calle], slot_name[current_callee], Q_ARG(QVariant, var));
    7. };
    8. private:
    9. QVector<QObject*> callee;
    10. QVector<QString> slot_name;
    11. int current_callee;
    12. };
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Signals and Slots

    Let's put the question a bit differently.
    If I connect many slots to one signal. What will happen each time signal gets emitted??

    Quote Originally Posted by Santosh Reddy View Post
    Instead have one network object with different signals

    signalSuccess1(QVariant)
    signalSuccess2(QVariant)
    signalSuccess3(QVariant)
    ..
    I will think about your suggestion, but then I may need to pass somehow some parameter to the
    MyNetworkClass object which will tell it which signal to emit in the requestFinished??? (I am not sure if
    it will cause some problem if some network calls are made in parallel etc.).

  6. #5
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots

    Quote Originally Posted by ggdev001 View Post
    Let's put the question a bit differently.
    If I connect many slots to one signal. What will happen each time signal gets emitted??
    Every connected slot will be emitted once in no particular order.

    Edit:
    Since Qt 4.6, as Lesiok correctly points out, slots are called in the same order as they were connected.
    Last edited by lanz; 20th February 2013 at 14:39.

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Signals and Slots

    Quote Originally Posted by lanz View Post
    in no particular order.
    That is true, but I remember that I saw some time ago, I think it was in the source code(?), that the connect statements will be executed in order they have been established. The reason what was given for that was, that if this would change, most applications would break because nobody cares about the information you gave So it is kind of a self made standard...

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Signals and Slots

    Quote Originally Posted by lanz View Post
    Every connected slot will be emitted once in no particular order.
    FALSE ! From Qt doc :
    If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted.

Similar Threads

  1. Connecting QML signals with Qt slots
    By KIBSOFT in forum Qt Quick
    Replies: 1
    Last Post: 15th November 2010, 10:18
  2. Replies: 8
    Last Post: 18th July 2009, 16:57
  3. Connecting signals and slots help pls
    By bod in forum Qt Programming
    Replies: 9
    Last Post: 1st July 2008, 16:01
  4. problem with connecting signals with slots"
    By impeteperry in forum Qt Programming
    Replies: 2
    Last Post: 27th September 2007, 17:44
  5. Connecting signals & slots across different threads
    By jyoti kumar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 13:40

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.