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

QPushButton released() runs twice because of focus change

Scheduled Pinned Locked Moved Solved Mobile and Embedded
qpushbuttonpressedreleased
6 Posts 2 Posters 734 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.
  • JeKK666J Offline
    JeKK666J Offline
    JeKK666
    wrote on 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...

    JonBJ 2 Replies Last reply
    1
    • JeKK666J JeKK666

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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.

      JeKK666J 1 Reply Last reply
      2
      • JeKK666J JeKK666

        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;
        }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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?

        JeKK666J 1 Reply Last reply
        0
        • JonBJ JonB

          @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?

          JeKK666J Offline
          JeKK666J Offline
          JeKK666
          wrote on 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...

          JonBJ 1 Reply Last reply
          0
          • JeKK666J JeKK666

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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.

            JeKK666J 1 Reply Last reply
            2
            • JonBJ JonB

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

              JeKK666J Offline
              JeKK666J Offline
              JeKK666
              wrote on 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
              • JeKK666J JeKK666 has marked this topic as solved on
              • JeKK666J JeKK666

                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;
                }
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on 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

                • Login

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