PDA

View Full Version : Slot executed 2 times on button press using connect / signal clicked()



emp1953
8th January 2019, 15:20
I am using QT 4.8.5 with Creator 3.0.1, RedHat 5

I believe this is a bug with QT 4.8.5. Offers of workarounds on this forum point to it being a bug. I don't know if later versions of Qt addressed it because I am not allowed to upgrade to a later version of Qt at this time.

I created slots using creator. added the button connect code and it executed the slot two times, every time

I created an app outside of creator doing everything manually with the same results. My final fix was to put a static flag in the slot, incrementing it after the slot executed one time, then checking the flag for zero upon reentry to the slot.
If > 0 reset it to zero and return out of the slot without executing it.

Thanks All

-emp1953

d_stranz
8th January 2019, 16:31
A slot is executed only once for each connection to it. That's the way it has been for as long as I have been using Qt, 4.8 included. At some point in Qt development the guarantee that slots would execute in the order they were connected was added, but I don't remember which version that was.

If you are getting double calls, then you have two connections. Check to be sure that:

1 - You didn't inadvertently make a connection when you were designing the form with Qt Designer (using the signals and slots editor) and then add another via code
2 - You didn't accidentally make a second connection from somewhere else in your code
3 - You aren't calling the slot manually from somewhere (or via a signal that results in the slot being called again).
4 - You didn't accidentally create two instances of the same object and are actually looking at the same slot code being called for different objects

You can use qDebug() statements in the slot to examine "this" (to ensure that (4) isn't the problem) and also use qDebug() to examine QObject::sender() inside the slot to see where the signal originates.

anda_skoa
11th January 2019, 18:32
Additional to the 4 possible causes listed by d_stranz:

5) you accidentally called your slot on_nameofbutton_clicked(), thus triggering the "connect by name" feature in setupUi()

Cheers,
_

emp1953
15th January 2019, 18:51
I did do " 1 - You didn't inadvertently make a connection when you were designing the form with Qt Designer (using the signals and slots editor) and then add another via code " above. So that was the culprit.
How do I break that connection once made while designing the form in Designer / creator? Other than simply giving the slot another name.

Thank you for that nugget of info.

d_stranz
16th January 2019, 00:11
How do I break that connection once made while designing the form

I have learned (the hard way, like you) to make all of my connections in code, as well as to never name my slots like anda_skoa's #5.

You can do one of the following:

1) Run Qt Designer again on your form, go to the signals / slots editor, and delete the connection
2) Open the .ui file for your form in a text editor. UI files are XML. Near the bottom of the file you should see a <connections> XML tag. Inside that block, you should see a <connection> tag for your button. Delete that whole tag (from <connection> through to the matching </connection>). Save the file.

If you manually edit the XML, be very careful not to delete anything else and be sure you delete matching open / close tags (i.e. <tag> ... </tag>) and don't leave anything dangling.

Giving the slot another name will give you a persistent runtime warning because the UI will still try to make the connection but won't be able to find it. Better to get rid of the duplicate.