Results 1 to 6 of 6

Thread: Signal - Slot connection inside loop

  1. #1
    Join Date
    Jan 2010
    Location
    Turkey
    Posts
    39
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Signal - Slot connection inside loop

    In my code I am creating new objects of same type inside loop and connecting a signal to object slot. Here is my trial.

    Qt Code:
    1. A * a;
    2. QList<A *> aList;
    3. int aCounter = 0;
    4. while(aCounter < 2)
    5. {
    6. a = new A;
    7. aList.push_back(a);
    8. connect(this,SIGNAL(somethingHappened()),aList[aCounter],SLOT(doSometing()));
    9. aCounter++;
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    When somethingHappened signal is emitted. Both objects slot is called. But I need to handle them seperately. Is it wrong to connect signal to a slot inside loop? If not how can I achieve my desire?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal - Slot connection inside loop

    You are connecting the signal to both objects, so yes, both objects will be called when you emit that signal.

    If you want to handle them seperately, you would need different signal for each object.

    Or just call the method in the object directly.

  3. #3
    Join Date
    Oct 2008
    Location
    Belgium
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Signal - Slot connection inside loop

    Hi, take a look at QSignalMapper, maybe that's what you're looking for?

    See http://doc.trolltech.com/4.6/qsignalmapper.html

    Good luck!

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

    Default Re: Signal - Slot connection inside loop

    in addition to the post above me, you can ask sender() in the slot.
    ==========================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.

  5. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Signal - Slot connection inside loop

    How the code is written, the sender() will be the same in each case, so I don't see how that would work? Only the recipient changes.

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

    Default Re: Signal - Slot connection inside loop

    Then I fail to understand the problem to begin with.
    If you have one sender, which you connected to many objects with one signal, then all the objects will react to that signal.
    What is the behavior you are after?
    ==========================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.

Similar Threads

  1. Signal and Slot connection not working !!
    By prakash437 in forum Qt Programming
    Replies: 3
    Last Post: 17th May 2010, 10:16
  2. Signal and slot connection graph
    By fahlen in forum General Discussion
    Replies: 4
    Last Post: 27th November 2007, 13:47
  3. signal and slot across threads having event loop
    By travis in forum Qt Programming
    Replies: 6
    Last Post: 5th November 2007, 23:56
  4. signal slot connection
    By Big Duck in forum Newbie
    Replies: 2
    Last Post: 4th July 2006, 13:31
  5. Replies: 4
    Last Post: 23rd January 2006, 16:51

Tags for this Thread

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.