Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Slot called twice when button pressed

Slot called twice when button pressed

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 10 Posters 13.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PetQter
    wrote on 10 Apr 2019, 19:34 last edited by
    #1

    Hello,
    I have weird problem. My application contains dialog with push buttons. I created slot for signal pressed() in QtCreator designer.
    After application startup I press button and slot is called once. But on next clicks on button it is called always two times.
    When I put Sleep(milisec) into slot and sleep time is >= 40ms then all is ok and slot is called once after click. When sleep time is less than 40ms then slot is called two times.
    Normally I start modeless dialog in slot so result is that I have opened two modeless dialogs after button is pressed.
    I investigated stack and see that it is always on the same functions in three levels back (moc_...cpp) .
    I created new button and behavior is the same.
    Must handling of pressed() signal take some minimal time ? How to solve it without Sleep() call ?

    I use Qt 5.12 and application is 32-bit running on 64-bit Win10, built with VS2017.

    A 1 Reply Last reply 10 Apr 2019, 19:44
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Apr 2019, 19:36 last edited by
      #2

      Hi,

      Did you check that you are not connecting that signal twice ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply 17 May 2024, 07:24
      2
      • P PetQter
        10 Apr 2019, 19:34

        Hello,
        I have weird problem. My application contains dialog with push buttons. I created slot for signal pressed() in QtCreator designer.
        After application startup I press button and slot is called once. But on next clicks on button it is called always two times.
        When I put Sleep(milisec) into slot and sleep time is >= 40ms then all is ok and slot is called once after click. When sleep time is less than 40ms then slot is called two times.
        Normally I start modeless dialog in slot so result is that I have opened two modeless dialogs after button is pressed.
        I investigated stack and see that it is always on the same functions in three levels back (moc_...cpp) .
        I created new button and behavior is the same.
        Must handling of pressed() signal take some minimal time ? How to solve it without Sleep() call ?

        I use Qt 5.12 and application is 32-bit running on 64-bit Win10, built with VS2017.

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 10 Apr 2019, 19:44 last edited by aha_1980 4 Oct 2019, 19:45
        #3

        @PetQter And just to add to @SGaist, how is your slot called (named)?

        If it is named like on_pushButton_clicked(), then it is auto-connected to the button: https://doc.qt.io/qt-5/designer-using-a-ui-file.html#automatic-connections which can easily lead to a double connection.

        Regards

        Qt has to stay free or it will die.

        1 Reply Last reply
        1
        • P Offline
          P Offline
          PetQter
          wrote on 10 Apr 2019, 20:07 last edited by
          #4

          I created it with qt designer, I did not write my own connect(…); And like I wrote - the same behavior is after I create new button and slot() with "Go To Slot..." command from context menu.
          Button has name buttonShowTcpConn,
          and slot declaration added by designer is:
          void on_buttonShowTcpConn_pressed();

          What exactly means "easily lead to a double connection" ?

          J 1 Reply Last reply 11 Apr 2019, 04:29
          0
          • P PetQter
            10 Apr 2019, 20:07

            I created it with qt designer, I did not write my own connect(…); And like I wrote - the same behavior is after I create new button and slot() with "Go To Slot..." command from context menu.
            Button has name buttonShowTcpConn,
            and slot declaration added by designer is:
            void on_buttonShowTcpConn_pressed();

            What exactly means "easily lead to a double connection" ?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 11 Apr 2019, 04:29 last edited by
            #5

            @PetQter said in Slot called twice when button pressed:

            What exactly means "easily lead to a double connection" ?

            Slots named like on_SOMETHING are autoconnected and if you then manually connect them via connect() you get them connected twice.

            On which device do you execute your app? Do you use touch screen?

            "I investigated stack and see that it is always on the same functions in three levels back (moc_...cpp)" - can you show the code from there?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • P Offline
              P Offline
              PetQter
              wrote on 11 Apr 2019, 05:22 last edited by
              #6

              Like I described in above posts - I did not use my own connect() I use designer so it is auto-connected. To exclude some other things done in the past I created new pushButton with slot for debugging purpose and behavior is the same.
              I use notebook with USB mouse I have not touch screen. Stack screenshots:

              0_1554960102132_Stack1.jpg
              0_1554960128031_Stack2.jpg
              0_1554960149075_Stack3.jpg

              J 1 Reply Last reply 11 Apr 2019, 05:33
              0
              • P PetQter
                11 Apr 2019, 05:22

                Like I described in above posts - I did not use my own connect() I use designer so it is auto-connected. To exclude some other things done in the past I created new pushButton with slot for debugging purpose and behavior is the same.
                I use notebook with USB mouse I have not touch screen. Stack screenshots:

                0_1554960102132_Stack1.jpg
                0_1554960128031_Stack2.jpg
                0_1554960149075_Stack3.jpg

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 11 Apr 2019, 05:33 last edited by J.Hilk 4 Nov 2019, 05:35
                #7

                @PetQter ok, the connect is indeed added twice in the moc file. on 2nd look, they are named slightly different and therefore shouldn't interfere with each other.

                Very strange, what version of QtCreator are you using?

                Is this still the case, if you clean your project, rerun qmake and rebuild?

                The easiest way to solve this is, not using the connect by name feature. But connecting the the signals of your UI elements with slots in cpp via Object::connect calls


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  PetQter
                  wrote on 11 Apr 2019, 05:59 last edited by
                  #8

                  And what about described Sleep() behavior with limit 40ms ? I have suspicion that problem is somewhere in internals where is triggered press button event and this code does not wait to release button and detect pressed button again. It is only way how can I explain correct behavior when Sleep(milisec) is for longer time than 40ms.

                  Second thing - the same behavior I have when created new widgets application and put to main window only one push button and create auto-connected slot.

                  J 1 Reply Last reply 11 Apr 2019, 06:15
                  0
                  • P Offline
                    P Offline
                    PetQter
                    wrote on 11 Apr 2019, 06:03 last edited by
                    #9

                    0_1554962615246_About.jpg

                    1 Reply Last reply
                    0
                    • P PetQter
                      11 Apr 2019, 05:59

                      And what about described Sleep() behavior with limit 40ms ? I have suspicion that problem is somewhere in internals where is triggered press button event and this code does not wait to release button and detect pressed button again. It is only way how can I explain correct behavior when Sleep(milisec) is for longer time than 40ms.

                      Second thing - the same behavior I have when created new widgets application and put to main window only one push button and create auto-connected slot.

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 11 Apr 2019, 06:15 last edited by
                      #10

                      @PetQter said in Slot called twice when button pressed:

                      And what about described Sleep() behavior with limit 40ms ?

                      signals aren't processed during sleep calls, so that is expected.

                      Like I said, try doing the connects by hand,

                      The connect by name functionality should be removed anyway, imho


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        PetQter
                        wrote on 11 Apr 2019, 07:19 last edited by
                        #11

                        I created new pushButton_2 and added: to MainWindow constructor:

                        QObject::connect(ui->pushButton_2, SIGNAL(pressed()), this, SLOT(pushButton_2_slot()));
                        
                        The result is the same, after click on pushButton_2 slot is called two times.
                        
                        J 1 Reply Last reply 11 Apr 2019, 07:39
                        0
                        • P PetQter
                          11 Apr 2019, 07:19

                          I created new pushButton_2 and added: to MainWindow constructor:

                          QObject::connect(ui->pushButton_2, SIGNAL(pressed()), this, SLOT(pushButton_2_slot()));
                          
                          The result is the same, after click on pushButton_2 slot is called two times.
                          
                          J Offline
                          J Offline
                          J.Hilk
                          Moderators
                          wrote on 11 Apr 2019, 07:39 last edited by
                          #12

                          @PetQter

                          ok, let's check if your mouse input is somehow messed up.

                          add the following in your constructor:

                          QPushButton *btn = new QPushButton();
                              btn->show();
                          
                              QObject::connect(btn, &QPushButton::pressed, btn, [=]()->void{qDebug() << "pressed" << QTime::currentTime();});
                              QObject::connect(btn, &QPushButton::released, btn, [=]()->void{qDebug() << "released" << QTime::currentTime();});
                              QObject::connect(btn, &QPushButton::clicked, btn, [=]()->void{qDebug() << "clicked" << QTime::currentTime();});
                          

                          you'll have to eventually include

                          #include <QDebug>
                          #include <QTime>
                          #include <QPushButton>

                          if it's not found.

                          This will create a standalone QpushButton that is shown and print mouse events to the QtCreator console.

                          you should see something like this

                          pressed QTime("09:35:33.590")
                          released QTime("09:35:33.721")
                          clicked QTime("09:35:33.721")

                          once for each mouse click on the button


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          1 Reply Last reply
                          3
                          • P Offline
                            P Offline
                            PetQter
                            wrote on 11 Apr 2019, 12:03 last edited by
                            #13

                            @J.Hilk said in Slot called twice when button pressed:

                            It seems to be ok, for example after last three clicks:

                            pressed QTime("14:01:05.558")
                            released QTime("14:01:05.636")
                            clicked QTime("14:01:05.636")
                            pressed QTime("14:01:13.449")
                            released QTime("14:01:13.559")
                            clicked QTime("14:01:13.559")
                            pressed QTime("14:01:15.684")
                            released QTime("14:01:15.809")
                            clicked QTime("14:01:15.809")

                            J 1 Reply Last reply 12 Apr 2019, 11:18
                            0
                            • P PetQter
                              11 Apr 2019, 12:03

                              @J.Hilk said in Slot called twice when button pressed:

                              It seems to be ok, for example after last three clicks:

                              pressed QTime("14:01:05.558")
                              released QTime("14:01:05.636")
                              clicked QTime("14:01:05.636")
                              pressed QTime("14:01:13.449")
                              released QTime("14:01:13.559")
                              clicked QTime("14:01:13.559")
                              pressed QTime("14:01:15.684")
                              released QTime("14:01:15.809")
                              clicked QTime("14:01:15.809")

                              J Offline
                              J Offline
                              J.Hilk
                              Moderators
                              wrote on 12 Apr 2019, 11:18 last edited by J.Hilk 4 Dec 2019, 11:57
                              #14

                              @PetQter
                              I'm afraid, than it very well may be something inside your code.

                              To nail it down on your particular project, I would suggest the following.

                              Create a new QWidget project. In the automatically created form layout add a single button.

                              inside the constructor of the form class (Most likely named MainWindow, if it's default)

                              add the same connects.

                              QObject::connect(ui->pushButton, &QPushButton::pressed, btn, [=]()->void{qDebug() << "pressed" << QTime::currentTime();});
                                  QObject::connect(ui->pushButton, &QPushButton::released, btn, [=]()->void{qDebug() << "released" << QTime::currentTime();});
                                  QObject::connect(ui->pushButton, &QPushButton::clicked, btn, [=]()->void{qDebug() << "clicked" << QTime::currentTime();});
                              

                              If it's also only printing once per click than it's something inside your code.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              3
                              • P Offline
                                P Offline
                                PetQter
                                wrote on 22 Apr 2019, 21:57 last edited by
                                #15

                                I created new project with button in MainWindow before my last post because I wanted to exclude if it is something in my code. Result is the same.
                                Try please to comment both connect with released and clicked events and leave only connect with pressed handler and put breakpoint into pressed handler. Does not stops your code on breakpoint two times ?
                                Try it please pushing button several times, in my case on first attempt it stops once and after F5 (continue) it does not stop again. But on several next attempts on every click it stops on breakpoint and after continue it stops on breakpoint second time.

                                J 1 Reply Last reply 23 Apr 2019, 05:27
                                0
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 22 Apr 2019, 22:06 last edited by
                                  #16

                                  Hi
                                  Only stops once here pr click. No matter how many times i click and f5.

                                  alt text

                                  1 Reply Last reply
                                  2
                                  • P PetQter
                                    22 Apr 2019, 21:57

                                    I created new project with button in MainWindow before my last post because I wanted to exclude if it is something in my code. Result is the same.
                                    Try please to comment both connect with released and clicked events and leave only connect with pressed handler and put breakpoint into pressed handler. Does not stops your code on breakpoint two times ?
                                    Try it please pushing button several times, in my case on first attempt it stops once and after F5 (continue) it does not stop again. But on several next attempts on every click it stops on breakpoint and after continue it stops on breakpoint second time.

                                    J Offline
                                    J Offline
                                    J.Hilk
                                    Moderators
                                    wrote on 23 Apr 2019, 05:27 last edited by J.Hilk
                                    #17

                                    @PetQter
                                    works fine for me
                                    https://vimeo.com/user97690670/review/331922108/c1f17dd078


                                    just tested it with pressed, same behavior as clicked in the video


                                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                    Q: What's that?
                                    A: It's blue light.
                                    Q: What does it do?
                                    A: It turns blue.

                                    1 Reply Last reply
                                    2
                                    • P Offline
                                      P Offline
                                      PetQter
                                      wrote on 24 Apr 2019, 05:28 last edited by
                                      #18

                                      I believe you. On my side it is probably depending on some internal dll responsible for events like mouse pressed. Maybe combination of Qt library, QtCreator version and Win 10 version. I must check it on another Windows developer's machine but it takes some days to have opportunity. I'd like to know if it has the same problem. I will write result to this post then.

                                      1 Reply Last reply
                                      0
                                      • K Offline
                                        K Offline
                                        KidTrent
                                        wrote on 18 Oct 2020, 19:28 last edited by
                                        #19

                                        Sorry for the necro, but I was having this same issue and I discovered it was because I was inheriting from another QDialog that I had made. Figured I would post and explain the issue...

                                        Steps to reproduce...

                                        • Make a base QDialog (let's call it BaseDialog).
                                        • Add a button to BaseDialog.
                                        • In QtDesigner, right click the button and click "Go to slot"->"clicked(bool)".
                                        • Add some debugging to the created slot so you can see it is being hit (qDebug("Slot Hit");).
                                        • Make another QDialog (SecondaryDialog).
                                        • Force SecondaryDialog to inherit from BaseDialog instead of QDialog (just edit the .h file of SecondaryDialog).
                                        • Create an instance of SecondaryDialog programmatically.
                                        • Run your program and click the button inside SecondaryDialog, it will be hit twice...

                                        I managed to solve this by manually adding the connect call in the BaseDialog constructor instead of depending on Qt to do it for me...

                                        JonBJ 1 Reply Last reply 18 Oct 2020, 22:04
                                        2
                                        • Christian EhrlicherC Online
                                          Christian EhrlicherC Online
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on 18 Oct 2020, 20:53 last edited by
                                          #20

                                          Please provide a minimal, compilable example of the issue. You for sure did the connect twice.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          1 Reply Last reply
                                          2

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved