Results 1 to 5 of 5

Thread: Slot executed 2 times on button press using connect / signal clicked()

  1. #1
    Join Date
    Apr 2017
    Posts
    55
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Slot executed 2 times on button press using connect / signal clicked()

    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

  2. #2
    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 executed 2 times on button press using connect / signal clicked()

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

  3. The following user says thank you to d_stranz for this useful post:

    emp1953 (15th January 2019)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Slot executed 2 times on button press using connect / signal clicked()

    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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    d_stranz (11th January 2019)

  6. #4
    Join Date
    Apr 2017
    Posts
    55
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slot executed 2 times on button press using connect / signal clicked()

    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.

  7. #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 executed 2 times on button press using connect / signal clicked()

    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.
    <=== 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. The following user says thank you to d_stranz for this useful post:

    emp1953 (30th April 2019)

Similar Threads

  1. Press one button - emit signal more time
    By SirJonas in forum Newbie
    Replies: 4
    Last Post: 25th October 2016, 10:03
  2. Replies: 0
    Last Post: 17th November 2015, 19:11
  3. How to connect a slot(QWidget*) with signal(clicked())
    By revellix in forum Qt Programming
    Replies: 10
    Last Post: 5th August 2011, 17:19
  4. Slot function not being executed in connect
    By Leirbag89 in forum Newbie
    Replies: 7
    Last Post: 19th May 2011, 20:53
  5. Qt bug? Signal emitted once, slot called multiple times
    By MattPhillips in forum Qt Programming
    Replies: 22
    Last Post: 1st December 2010, 23:32

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.