PDA

View Full Version : slot running recursively for no reason



TonyInSoMD
3rd August 2017, 14:08
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. :confused:

high_flyer
3rd August 2017, 15:05
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.

TonyInSoMD
3rd August 2017, 15:13
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.

high_flyer
3rd August 2017, 15:21
===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.

d_stranz
3rd August 2017, 18:36
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 (https://doc.qt.io/qt-5/signalsandslots.html):


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.

high_flyer
6th August 2017, 23:35
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.

d_stranz
7th August 2017, 18:27
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.

TonyInSoMD
8th August 2017, 01:57
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. :o