Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QPushButton released() runs twice because of focus change
QtWS25 Last Chance

QPushButton released() runs twice because of focus change

Scheduled Pinned Locked Moved Solved Mobile and Embedded
qpushbuttonpressedreleased
6 Posts 2 Posters 727 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.
  • J Offline
    J Offline
    JeKK666
    wrote on 11 May 2023, 12:27 last edited by
    #1

    I have a widget with 4 QPushBbuttons: i have used the QAbstarctButton::pressed() event to start a timer and count for a long press, and i have used the QAbstarctButton::released() event to react to a short press, if the long press was not triggered.

    Everything works, except one of the buttons' long press function is to display a non-modal QDialog: this causes the base widget to lose focus, in turn causing my button to emit an extra released() signal, causing its short press intended code to run twice: one time due to the focus change of the base widget, and the other being the actual user's finger leaving the touchscreen surface.

    Is there any way to bypass this behavior without changing the structure?

    Some code:

    ... //somewhere in main
    QTimer softKeypadTimer;
    connect(softKeypadTimer, SIGNAL(timeout()), this, SLOT(on_softKeypadTimeout()), Qt::UniqueConnection);
    int softKeypadKeyPressed;
    bool softKeypadLongpress;
    ...
    
    void MyWidget::on_btn_BottomRight_pressed() {
    	softKeypadTimer.start();
    	softKeypadKeyPressed = BOTTOM_RIGHT_LONGPRESS;
    }
    
    void MyWidget::on_softKeypadTimeout() { //connected to softKeypadTimer timeout signal
    	softKeypadLongpress = true; //block execution of shortpress routines
    	emit softKeyboard(softKeypadKeyPressed);
    }
    
    void MyWidget::on_btn_BottomRight_released() {
        softKeypadTimer.stop();
        if (!softKeypadLongpress)
            emit softKeyboard(BOTTOM_RIGHT);
        softKeypadLongpress = false;
    }
    

    Still haven't learned Lambdas...

    J 2 Replies Last reply 11 May 2023, 12:34
    1
    • J JeKK666
      11 May 2023, 12:45

      @JonB Yes, the widget containing the button, and therefore the button, lose focus when the QDialog is painted on screen.
      I can't ignore the released() event, because it handles the short press functionality of the QPushButtonin the GUI.

      J Offline
      J Offline
      JonB
      wrote on 11 May 2023, 12:52 last edited by
      #4

      @JeKK666
      You are only supposed to ignore it in the " one of the buttons' long press function is to display a non-modal QDialog" case, where (I believe) you say you will get the "genuine" release later when the user actually removes his finger or something. Whatever it takes to recognise this is a focus change from the dialog which will later result in another released from the finger. Something like that.

      J 1 Reply Last reply 11 May 2023, 13:00
      2
      • J JeKK666
        11 May 2023, 12:27

        I have a widget with 4 QPushBbuttons: i have used the QAbstarctButton::pressed() event to start a timer and count for a long press, and i have used the QAbstarctButton::released() event to react to a short press, if the long press was not triggered.

        Everything works, except one of the buttons' long press function is to display a non-modal QDialog: this causes the base widget to lose focus, in turn causing my button to emit an extra released() signal, causing its short press intended code to run twice: one time due to the focus change of the base widget, and the other being the actual user's finger leaving the touchscreen surface.

        Is there any way to bypass this behavior without changing the structure?

        Some code:

        ... //somewhere in main
        QTimer softKeypadTimer;
        connect(softKeypadTimer, SIGNAL(timeout()), this, SLOT(on_softKeypadTimeout()), Qt::UniqueConnection);
        int softKeypadKeyPressed;
        bool softKeypadLongpress;
        ...
        
        void MyWidget::on_btn_BottomRight_pressed() {
        	softKeypadTimer.start();
        	softKeypadKeyPressed = BOTTOM_RIGHT_LONGPRESS;
        }
        
        void MyWidget::on_softKeypadTimeout() { //connected to softKeypadTimer timeout signal
        	softKeypadLongpress = true; //block execution of shortpress routines
        	emit softKeyboard(softKeypadKeyPressed);
        }
        
        void MyWidget::on_btn_BottomRight_released() {
            softKeypadTimer.stop();
            if (!softKeypadLongpress)
                emit softKeyboard(BOTTOM_RIGHT);
            softKeypadLongpress = false;
        }
        
        J Offline
        J Offline
        JonB
        wrote on 11 May 2023, 12:34 last edited by
        #2

        @JeKK666 said in QPushButton released() runs twice because of focus change:

        one time due to the focus change of the base widget

        You are saying this happens when, is it when the QDialog gets displayed? If you don't get a better solution could you ignore that released() signal case?

        J 1 Reply Last reply 11 May 2023, 12:45
        0
        • J JonB
          11 May 2023, 12:34

          @JeKK666 said in QPushButton released() runs twice because of focus change:

          one time due to the focus change of the base widget

          You are saying this happens when, is it when the QDialog gets displayed? If you don't get a better solution could you ignore that released() signal case?

          J Offline
          J Offline
          JeKK666
          wrote on 11 May 2023, 12:45 last edited by
          #3

          @JonB Yes, the widget containing the button, and therefore the button, lose focus when the QDialog is painted on screen.
          I can't ignore the released() event, because it handles the short press functionality of the QPushButtonin the GUI.

          Still haven't learned Lambdas...

          J 1 Reply Last reply 11 May 2023, 12:52
          0
          • J JeKK666
            11 May 2023, 12:45

            @JonB Yes, the widget containing the button, and therefore the button, lose focus when the QDialog is painted on screen.
            I can't ignore the released() event, because it handles the short press functionality of the QPushButtonin the GUI.

            J Offline
            J Offline
            JonB
            wrote on 11 May 2023, 12:52 last edited by
            #4

            @JeKK666
            You are only supposed to ignore it in the " one of the buttons' long press function is to display a non-modal QDialog" case, where (I believe) you say you will get the "genuine" release later when the user actually removes his finger or something. Whatever it takes to recognise this is a focus change from the dialog which will later result in another released from the finger. Something like that.

            J 1 Reply Last reply 11 May 2023, 13:00
            2
            • J JonB
              11 May 2023, 12:52

              @JeKK666
              You are only supposed to ignore it in the " one of the buttons' long press function is to display a non-modal QDialog" case, where (I believe) you say you will get the "genuine" release later when the user actually removes his finger or something. Whatever it takes to recognise this is a focus change from the dialog which will later result in another released from the finger. Something like that.

              J Offline
              J Offline
              JeKK666
              wrote on 11 May 2023, 13:00 last edited by
              #5

              @JonB Yes, this was the solution, in the end: my issue was that the 4 buttons have swappable functions, so i could not programmatically know which one would end up calling for the dialog to display.
              So i've added a dedicated flag and i set it in the code that would make the dialog visible, to allow skipping of the unwanted event, it works :)

              Thanks @JonB.

              Still haven't learned Lambdas...

              1 Reply Last reply
              2
              • J JeKK666 has marked this topic as solved on 11 May 2023, 13:00
              • J JeKK666
                11 May 2023, 12:27

                I have a widget with 4 QPushBbuttons: i have used the QAbstarctButton::pressed() event to start a timer and count for a long press, and i have used the QAbstarctButton::released() event to react to a short press, if the long press was not triggered.

                Everything works, except one of the buttons' long press function is to display a non-modal QDialog: this causes the base widget to lose focus, in turn causing my button to emit an extra released() signal, causing its short press intended code to run twice: one time due to the focus change of the base widget, and the other being the actual user's finger leaving the touchscreen surface.

                Is there any way to bypass this behavior without changing the structure?

                Some code:

                ... //somewhere in main
                QTimer softKeypadTimer;
                connect(softKeypadTimer, SIGNAL(timeout()), this, SLOT(on_softKeypadTimeout()), Qt::UniqueConnection);
                int softKeypadKeyPressed;
                bool softKeypadLongpress;
                ...
                
                void MyWidget::on_btn_BottomRight_pressed() {
                	softKeypadTimer.start();
                	softKeypadKeyPressed = BOTTOM_RIGHT_LONGPRESS;
                }
                
                void MyWidget::on_softKeypadTimeout() { //connected to softKeypadTimer timeout signal
                	softKeypadLongpress = true; //block execution of shortpress routines
                	emit softKeyboard(softKeypadKeyPressed);
                }
                
                void MyWidget::on_btn_BottomRight_released() {
                    softKeypadTimer.stop();
                    if (!softKeypadLongpress)
                        emit softKeyboard(BOTTOM_RIGHT);
                    softKeypadLongpress = false;
                }
                
                J Offline
                J Offline
                JonB
                wrote on 11 May 2023, 13:30 last edited by
                #6

                @JeKK666 said in QPushButton released() runs twice because of focus change:

                one of the buttons' long press function

                You actually get a bonus point for your use of the possessive apostrophe on this forum ;-)

                1 Reply Last reply
                0

                2/6

                11 May 2023, 12:34

                topic:navigator.unread, 4
                • Login

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