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. Press touch screen and hot plug the touch board, qt app(push button) cannot receive mousepress event, button click not work
Forum Update on Monday, May 27th 2025

Press touch screen and hot plug the touch board, qt app(push button) cannot receive mousepress event, button click not work

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 546 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.
  • JonBJ JonB

    @Zonghua-He said in Press touch screen and hot plug the touch board, qt app(push button) cannot receive mousepress event, button click not work:

    QTouchEvent touchEvent = static_cast<QTouchEvent>(event);

    One thing I don't get. How can you know that every MainWindow::event(QEvent *event) is going to be a QTouchEvent? Won't the static_cast barf if it is not?

    JoeCFDJ Offline
    JoeCFDJ Offline
    JoeCFD
    wrote on last edited by JoeCFD
    #6

    @JonB Good point!

    JonBJ 1 Reply Last reply
    0
    • JoeCFDJ JoeCFD

      @JonB Good point!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #7

      @JoeCFD
      Yes but I assumed (untested) that

      QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
      

      would static-cast-except if it is not a QTouchEvent? Oh, I can't remember any more, doesn't static_cast throw ab exception where dynamic_cast would (for a pointer) return nullptr? Or is static_cast just the dangerous C cast, no exception? Yeah, I'm beginning to think it's that....

      JoeCFDJ 1 Reply Last reply
      0
      • JonBJ JonB

        @JoeCFD
        Yes but I assumed (untested) that

        QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
        

        would static-cast-except if it is not a QTouchEvent? Oh, I can't remember any more, doesn't static_cast throw ab exception where dynamic_cast would (for a pointer) return nullptr? Or is static_cast just the dangerous C cast, no exception? Yeah, I'm beginning to think it's that....

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #8

        @JonB
        std::static_cast:
        No runtime checks: If you downcast a pointer to a derived class incorrectly, it leads to undefined behavior.
        Faster: No runtime overhead.

        std::dynamic_cast:
        Performs a runtime check to ensure the cast is valid.
        For pointers: nullptr if the cast fails.
        For references: Throws std::bad_cast if the cast fails.
        Involves runtime type information (RTTI) overhead.

        I think std::dynamic_cast is needed.

        JonBJ 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @JonB
          std::static_cast:
          No runtime checks: If you downcast a pointer to a derived class incorrectly, it leads to undefined behavior.
          Faster: No runtime overhead.

          std::dynamic_cast:
          Performs a runtime check to ensure the cast is valid.
          For pointers: nullptr if the cast fails.
          For references: Throws std::bad_cast if the cast fails.
          Involves runtime type information (RTTI) overhead.

          I think std::dynamic_cast is needed.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #9

          @JoeCFD
          OK, I think I was confusing some "static cast" in C# which did "assert" if incorrect. Got it.

          I don't think you can use dyanmic_cast here because event is not a pointer here? dynamic_cast<QTouchEvent>(event) would not be able to return its usual nullptr if wrong type, so I don't see how you can here?

          JoeCFDJ 1 Reply Last reply
          0
          • JonBJ JonB

            @JoeCFD
            OK, I think I was confusing some "static cast" in C# which did "assert" if incorrect. Got it.

            I don't think you can use dyanmic_cast here because event is not a pointer here? dynamic_cast<QTouchEvent>(event) would not be able to return its usual nullptr if wrong type, so I don't see how you can here?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #10

            @JonB bool MainWindow::event(QEvent *event)

            JonBJ 1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @JonB bool MainWindow::event(QEvent *event)

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #11

              @JoeCFD
              Here is is copied verbatim from what OP posted in https://forum.qt.io/post/823816 above:

              bool MainWindow::event(QEvent *event)
              {
              QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
              

              That is static_cast<QTouchEvent>, not static_cast<QTouchEvent *>. I think I get it. OP did not use ``` :( So the * asterisk for pointer did italic instead of visible. That's the problem with no Code tags :( I get it now, misunderstanding.

              1 Reply Last reply
              0
              • JonBJ JonB

                @Zonghua-He said in Press touch screen and hot plug the touch board, qt app(push button) cannot receive mousepress event, button click not work:

                QTouchEvent touchEvent = static_cast<QTouchEvent>(event);

                One thing I don't get. How can you know that every MainWindow::event(QEvent *event) is going to be a QTouchEvent? Won't the static_cast barf if it is not?

                Z Offline
                Z Offline
                Zonghua He
                wrote on last edited by
                #12

                @JonB Hi,thanks for your idea.

                My code is not safe. Should be:
                bool MainWindow::event(QEvent *event)
                {
                if (event->type() == QEvent::TouchBegin ||
                event->type() == QEvent::TouchUpdate ||
                event->type() == QEvent::TouchEnd) {
                QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
                qDebug() << "New event:" << touchEvent->type();

                    QObject *target = childAt(touchEvent->touchPoints().first().pos().toPoint());
                    qDebug() << "target: " << target;
                
                    if (qobject_cast<QPushButton*>(target) ||
                            qobject_cast<QRadioButton*>(target) ||
                            qobject_cast<QComboBox*>(target) ||
                            qobject_cast<QSlider*>(target) ||
                            qobject_cast<QLineEdit*>(target) ||
                            qobject_cast<QLabel*>(target) ||
                            qobject_cast<QScrollBar*>(target)) {
                        qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Include.";
                        return QMainWindow::event(event);
                    }
                
                    qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Will return true.";
                
                    return true;//if false, QPushButton will not be deliver
                }
                
                qDebug() << "----------------------------------------------------------------- Will goto last return.";
                
                return QMainWindow::event(event); 
                

                }

                Do you have any idea about my problem? After hot plug touch board usb cable(finger is touching on screen at the same time), all the pushbuttons, sliders and so on, can not response. But if i hot plug the touch board usb cable without touching the touch screen, all is well. This problem always occurs on widget app. The QML app works well.

                JonBJ 1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kevin Hoang
                  wrote on last edited by
                  #13

                  Based on the log you provided, the system correctly recognizes your touchscreen after reconnecting. However, Qt has detected the device as a touchscreen through libinput (event3). This means that Qt is handling touch events instead of mouse events.

                  You might want to try adding QCoreApplication::setAttribute(Qt::AA_SynthesizeMouseForUnhandledTouchEvents); in your main application to see if that resolves the issue.

                  Z 1 Reply Last reply
                  0
                  • K Kevin Hoang

                    Based on the log you provided, the system correctly recognizes your touchscreen after reconnecting. However, Qt has detected the device as a touchscreen through libinput (event3). This means that Qt is handling touch events instead of mouse events.

                    You might want to try adding QCoreApplication::setAttribute(Qt::AA_SynthesizeMouseForUnhandledTouchEvents); in your main application to see if that resolves the issue.

                    Z Offline
                    Z Offline
                    Zonghua He
                    wrote on last edited by
                    #14

                    @Kevin-Hoang said in Press touch screen and hot plug the touch board, qt app(push button) cannot receive mousepress event, button click not work:

                    orUnhandled

                    Hi Kevin Hoang, I did what you told me. But also not work.
                    My project is : https://github.com/hezonghua/linux_QT_touch_screen_widget_app

                    I will appreciate if you can have a look at my project. Thanks for advance.

                    1 Reply Last reply
                    0
                    • Z Zonghua He

                      @JonB Hi,thanks for your idea.

                      My code is not safe. Should be:
                      bool MainWindow::event(QEvent *event)
                      {
                      if (event->type() == QEvent::TouchBegin ||
                      event->type() == QEvent::TouchUpdate ||
                      event->type() == QEvent::TouchEnd) {
                      QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
                      qDebug() << "New event:" << touchEvent->type();

                          QObject *target = childAt(touchEvent->touchPoints().first().pos().toPoint());
                          qDebug() << "target: " << target;
                      
                          if (qobject_cast<QPushButton*>(target) ||
                                  qobject_cast<QRadioButton*>(target) ||
                                  qobject_cast<QComboBox*>(target) ||
                                  qobject_cast<QSlider*>(target) ||
                                  qobject_cast<QLineEdit*>(target) ||
                                  qobject_cast<QLabel*>(target) ||
                                  qobject_cast<QScrollBar*>(target)) {
                              qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Include.";
                              return QMainWindow::event(event);
                          }
                      
                          qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Will return true.";
                      
                          return true;//if false, QPushButton will not be deliver
                      }
                      
                      qDebug() << "----------------------------------------------------------------- Will goto last return.";
                      
                      return QMainWindow::event(event); 
                      

                      }

                      Do you have any idea about my problem? After hot plug touch board usb cable(finger is touching on screen at the same time), all the pushbuttons, sliders and so on, can not response. But if i hot plug the touch board usb cable without touching the touch screen, all is well. This problem always occurs on widget app. The QML app works well.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #15

                      @Zonghua-He
                      Please use the forum posts' Code tags (the </> button, or a line of ``` above and below) when posting blocks of code if you wish to avoid the incorrect code you show and my misunderstanding about what you actually have.

                      I am sorry but I do not know about your problem.

                      Z 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @Zonghua-He
                        Please use the forum posts' Code tags (the </> button, or a line of ``` above and below) when posting blocks of code if you wish to avoid the incorrect code you show and my misunderstanding about what you actually have.

                        I am sorry but I do not know about your problem.

                        Z Offline
                        Z Offline
                        Zonghua He
                        wrote on last edited by
                        #16

                        @JonB Thanks for your advice.

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          Kevin Hoang
                          wrote on last edited by
                          #17

                          I really don’t have time to help you, but I noticed that when you unplug the screen, the TouchCancel event is triggered (TouchCancel also causes MouseButtonRelease to be sent to all widgets for processing). I suspect that during the TouchCancel process, something gets stuck, preventing proper event handling when you plug the screen back in.

                          Since the Qt version you’re using is quite old, I’m not sure if there’s any existing bug related to this.

                          You might try calling QApplication::processEvents() in the screenAdd event and see if that helps.

                          Also, before unplugging the screen (when everything is working fine), check whether the Input DeviceType is TouchScreen and compare if there are any differences between the initial state of the application and the state after hot-plugging.

                          Z 1 Reply Last reply
                          0
                          • K Kevin Hoang

                            I really don’t have time to help you, but I noticed that when you unplug the screen, the TouchCancel event is triggered (TouchCancel also causes MouseButtonRelease to be sent to all widgets for processing). I suspect that during the TouchCancel process, something gets stuck, preventing proper event handling when you plug the screen back in.

                            Since the Qt version you’re using is quite old, I’m not sure if there’s any existing bug related to this.

                            You might try calling QApplication::processEvents() in the screenAdd event and see if that helps.

                            Also, before unplugging the screen (when everything is working fine), check whether the Input DeviceType is TouchScreen and compare if there are any differences between the initial state of the application and the state after hot-plugging.

                            Z Offline
                            Z Offline
                            Zonghua He
                            wrote on last edited by
                            #18

                            @Kevin-Hoang Hi,i tried to call QApplication::processEvents() in the screenAdd event,i found that it has no help. It still does not work. Thanks for your suggestion.

                            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