Results 1 to 9 of 9

Thread: Confusing Signal/Slot behaviour, more than one slot firing, why?

  1. #1
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Confusing Signal/Slot behaviour, more than one slot firing, why?

    Hi, I was wondering if someone could clear something up for me here. I have a User class that holds basic info like name etc that I want to load per user, it another function I have linked up some the signals emitted from the class to the slots in the constrctor. It looks something like this.

    Qt Code:
    1. connect(userA, SIGNAL(isLoaded()), this, SLOT(onALoaded()));
    2. connect(userB, SIGNAL(isLoaded()), this, SLOT(onBLoaded()));
    3. }
    4.  
    5. UserData* userA;
    6. UserData* userB
    7.  
    8. private slots:
    9. void onALoaded()
    10. {
    11. //some A stuff
    12. }
    13.  
    14. void onBLoaded()
    15. {
    16. //some B stuff
    17. }
    To copy to clipboard, switch view to plain text mode 

    What I am finding is that both slots are firing even though the signal originates from two different objects. This obviously cannot be correct, this would mean every push button defined would fire every slot a push button is connected too, so what could have gone wrong in my case?

    Thanks
    Last edited by hybrid_snyper; 6th November 2012 at 15:26.

  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: Confusing Signal/Slot behaviour, more than one slot firing, why?

    What I am finding is that both slots are firing even though the signal originates from two different objects.
    Can you reword this (it looks contradicting statement to me)

    If both the object emit signal, both slots will be called.
    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. #3
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Confusing Signal/Slot behaviour, more than one slot firing, why?

    Quote Originally Posted by Santosh Reddy View Post
    Can you reword this (it looks contradicting statement to me)

    If both the object emit signal, both slots will be called.
    Sorry, if isLoaded() fires from userA then both methods, onALoaded() and onBLoaded() run. I would expect onALoaded() to be the only slot that is ran.

  4. #4
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Confusing Signal/Slot behaviour, more than one slot firing, why?

    It looks like you declare userA and userB after making the connect statements. Is that causing some of your issues? I think we need to see more code to know what exactly is going on.

  5. #5
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Confusing Signal/Slot behaviour, more than one slot firing, why?

    Ok, below is the class simplified with the content and structure that is relevant to my issue. All compiles fine and runs only problem I am having is the Signal/Slot issue.

    Qt Code:
    1. class myWidget::myWidgetPrivate : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. myWidgetPrivate(myWidget* p)
    7. : parent(p)
    8. {
    9. userA = new UserData;
    10. connect(userA, SIGNAL(isLoaded()), this, SLOT(onALoaded()));
    11.  
    12. userB = new UserData;
    13. connect(userB, SIGNAL(isLoaded()), this, SLOT(onBLoaded()));
    14. }
    15.  
    16. UserData* userA;
    17. UserData* userB;
    18.  
    19. private slots:
    20.  
    21. void onALoaded()
    22. {
    23. //Do some stuff with A
    24. }
    25.  
    26. void onBLoaded()
    27. {
    28. //Do some stuff with B
    29. }
    30. };
    31.  
    32. #include "myWidget.moc"
    33.  
    34. myWidget::myWidget(QWidget *parent)
    35. : QWidget( parent ),
    36. d( new myWidgetPrivate(this) )
    37. {}
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Confusing Signal/Slot behaviour, more than one slot firing, why?

    Are you manually emitting isLoaded() signal? If so, where are you emitting this signal from and if not, what class is UserData based off? The code above looks fine, but we are not seeing when/how the signals are being emitted. Can we see where you are emitting the signals to ensure you are not accidentally emitting them for both objects?

  7. #7
    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: Confusing Signal/Slot behaviour, more than one slot firing, why?

    Do find any warnings in Application output area?
    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.

  8. #8
    Join Date
    Oct 2009
    Posts
    22
    Thanks
    2
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Confusing Signal/Slot behaviour, more than one slot firing, why?

    Quote Originally Posted by ToddAtWSU View Post
    Are you manually emitting isLoaded() signal? If so, where are you emitting this signal from and if not, what class is UserData based off? The code above looks fine, but we are not seeing when/how the signals are being emitted. Can we see where you are emitting the signals to ensure you are not accidentally emitting them for both objects?
    I think I have figured it out, I have another class that is emitting a signal that the UserData class is waiting for. This means that each instance of UserData is firing slots due to a signal from another class. The crude diagram below should highlight what I mean.

    Controller -------- signal--------|- UserData A ---signal-- myWidget:nA
    |- UserData B ---signal-- myWidget:nB


    Sorry for wasting your time, but you have helped me to work the problem.

    Thanks.

  9. #9
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Confusing Signal/Slot behaviour, more than one slot firing, why?

    Not a problem. I solve most of my problems by trying to walk someone else through it and before I finish explaining it, I solve it. So I am glad you figured it out!

Similar Threads

  1. Replies: 2
    Last Post: 3rd May 2011, 21:22
  2. QProcess finished slot not firing
    By doggrant in forum Qt Programming
    Replies: 0
    Last Post: 10th November 2010, 14:09
  3. Wierd behaviour in a slot routine
    By koenig in forum Newbie
    Replies: 7
    Last Post: 29th January 2010, 08:27
  4. pointers behaviour in qt and specially signals/slot mechanism
    By salmanmanekia in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2008, 20:34
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 19:52

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.