Results 1 to 8 of 8

Thread: slot running recursively for no reason

  1. #1
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default slot running recursively for no reason

    I've got my main class, CRat. During construction I create a second QMainWindow and run show(). I want both windows to be open at the same time, so it's non-modal. The second window has buttons connected to slots. If I click on a button, the slot will run but it will repeat itself at least twice. I've gone over my code with a fine tooth comb, the slot is not being called again, but as soon as it gets to the end of the slot, the slot reruns anyway.

  2. #2
    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: slot running recursively for no reason

    Impossible to tell without seeing some relevant code.
    Relevant code would be the slot it self, the method with the connect to the slot is made.
    ==========================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
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: slot running recursively for no reason

    I found the problem. I have a function called SetupConnections(). It runs connect for all the buttons, etc. I was calling it twice in the constructor. So all the connections were being made twice. Since the buttons were connected to their slots twice, the slot would run twice. Short version, user error. The user has to be more intelligent then the equipment he's working on. It took me two weeks to find this. And of course when I finally give up and post a question, I find the answer. Go figure.

  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: slot running recursively for no reason

    ===The information in this post is WRONG! I am leaving it here for sake of thread coherency please read the answer by d_stranz below===================

    I was calling it twice in the constructor. So all the connections were being made twice.
    Nope.
    That CAN'T be the reason since the number of times you connect a signal and slot does NOT influence the times a slot will be called. (presuming the same caller and receiver and same signal and slot in the connect call)
    What will effect the number of times a slot is called is the number of times the triggering signal is emitted.
    Last edited by high_flyer; 6th August 2017 at 22:37.
    ==========================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
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: slot running recursively for no reason

    That CAN'T be the reason since the number of times you connect a signal and slot does NOT influence the times a slot will be called. (presuming the same caller and receiver and same signal and slot in the connect call)
    Don't think that's quite correct. This from the Qt Signals and Slots documentation:

    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.
    and

    By default, for every connection you make, a signal is emitted; two signals are emitted for duplicate connections. You can break all of these connections with a single disconnect() call. If you pass the Qt::UniqueConnection type, the connection will only be made if it is not a duplicate. If there is already a duplicate (exact same signal to the exact same slot on the same objects), the connection will fail and connect will return false.
    It's probably in what you mean by "signal" - I generally think of emitting a signal as being an "emit" call in code. The event loop does turn this into a series of signal -> slot invocations, but they are all triggered by a single action at the user level. The documentation confuses this in the two quotes.

    So if the OP did call connect() twice with all the same parameters, he will get a duplicate connection and see the same slot being executed twice for a single UI action.
    <=== 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.

  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: slot running recursively for no reason

    wohaa....
    I stand corrected, bigly!
    I just tested it, and you are right d_stranz.
    I only ask my self how come I get something like that wrong after all these years working with Qt...
    Well, maybe because I never had that problem so far, but this tells me I need to look up the moc and metaobject again.

    Thanks for the correction, and to the OP - sorry for the misinformation!
    I think its my first time to really be sure of something in Qt and being so wrong about it.
    ==========================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.

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: slot running recursively for no reason

    I only ask my self how come I get something like that wrong after all these years working with Qt...
    I know it only because I made the same mistake as the OP, sort of. I used Qt Designer to make an auto-connection between a menu and a slot, and then I made the same connection again in code. I couldn't understand why the slot was running twice and it took a long time in the debugger to understand what was going on.

    Now, I never use Qt Designer to automatically connect signals and slots; everything is done in my code so I know what is going on.
    <=== 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.

  8. #8
    Join Date
    Mar 2014
    Location
    USA
    Posts
    85
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: slot running recursively for no reason

    Thanks d_stranz. You've helped me a lot with another problem I had recently. Knowing I'm not the only one this has happened and took a long time to find helps me feel not QUITE as stupid. Although I don't have a good excuse like you do. I did both of them in my code.

Similar Threads

  1. Fast solution to list directories recursively
    By stereoMatching in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2014, 16:47
  2. QSettings : rename/copy a group recursively
    By Kouillo in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2012, 21:51
  3. Replies: 25
    Last Post: 7th November 2012, 16:23
  4. Replies: 0
    Last Post: 23rd November 2011, 15:14
  5. Recursively changing margins/padding
    By ghorwin in forum Qt Programming
    Replies: 3
    Last Post: 4th September 2007, 11:08

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.