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

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 541 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.
  • Z Zonghua He
    2 Apr 2025, 10:13

    Hi JoeCFD, thanks for advice.

    AI told me to reimplement event():
    //reimplement event()
    bool MainWindow::event(QEvent *event)
    {
    QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
    qDebug() << "New event:" << touchEvent->type();

    if (event->type() == QEvent::TouchBegin ||
        event->type() == QEvent::TouchUpdate ||
        event->type() == QEvent::TouchEnd) {
    
        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 QWidget::event(event);
        }
    
        qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Will return true.";
    
        return true;//if false,  event to QPushButton will not be deliver
    }
    
    qDebug() << "----------------------------------------------------------------- Will goto last return.";
    
    return QMainWindow::event(event); 
    

    }

    QPushButton,QRadioButton,QComboBox,QSlider,QLineEdit,QLabel,QScrollBar can work well.

    But if the clicked object is QTableWidget, it will not work.

    I have no idea how to fix it.

    J Offline
    J Offline
    JoeCFD
    wrote on 2 Apr 2025, 11:17 last edited by
    #4

    @Zonghua-He Continue asking deepseek or grok.

    1 Reply Last reply
    0
    • Z Zonghua He
      2 Apr 2025, 10:13

      Hi JoeCFD, thanks for advice.

      AI told me to reimplement event():
      //reimplement event()
      bool MainWindow::event(QEvent *event)
      {
      QTouchEvent touchEvent = static_cast<QTouchEvent>(event);
      qDebug() << "New event:" << touchEvent->type();

      if (event->type() == QEvent::TouchBegin ||
          event->type() == QEvent::TouchUpdate ||
          event->type() == QEvent::TouchEnd) {
      
          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 QWidget::event(event);
          }
      
          qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Will return true.";
      
          return true;//if false,  event to QPushButton will not be deliver
      }
      
      qDebug() << "----------------------------------------------------------------- Will goto last return.";
      
      return QMainWindow::event(event); 
      

      }

      QPushButton,QRadioButton,QComboBox,QSlider,QLineEdit,QLabel,QScrollBar can work well.

      But if the clicked object is QTableWidget, it will not work.

      I have no idea how to fix it.

      J Offline
      J Offline
      JonB
      wrote on 2 Apr 2025, 13:08 last edited by
      #5

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

      J Z 2 Replies Last reply 2 Apr 2025, 13:53
      1
      • J JonB
        2 Apr 2025, 13:08

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

        J Offline
        J Offline
        JoeCFD
        wrote on 2 Apr 2025, 13:53 last edited by JoeCFD 4 Feb 2025, 13:53
        #6

        @JonB Good point!

        J 1 Reply Last reply 2 Apr 2025, 13:56
        0
        • J JoeCFD
          2 Apr 2025, 13:53

          @JonB Good point!

          J Offline
          J Offline
          JonB
          wrote on 2 Apr 2025, 13:56 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....

          J 1 Reply Last reply 2 Apr 2025, 14:08
          0
          • J JonB
            2 Apr 2025, 13:56

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

            J Offline
            J Offline
            JoeCFD
            wrote on 2 Apr 2025, 14:08 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.

            J 1 Reply Last reply 2 Apr 2025, 15:39
            0
            • J JoeCFD
              2 Apr 2025, 14:08

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

              J Offline
              J Offline
              JonB
              wrote on 2 Apr 2025, 15:39 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?

              J 1 Reply Last reply 2 Apr 2025, 16:12
              0
              • J JonB
                2 Apr 2025, 15:39

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

                J Offline
                J Offline
                JoeCFD
                wrote on 2 Apr 2025, 16:12 last edited by
                #10

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

                J 1 Reply Last reply 2 Apr 2025, 16:44
                0
                • J JoeCFD
                  2 Apr 2025, 16:12

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 2 Apr 2025, 16:44 last edited by JonB 4 Feb 2025, 16:45
                  #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
                  • J JonB
                    2 Apr 2025, 13:08

                    @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 3 Apr 2025, 00:58 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.

                    J 1 Reply Last reply 3 Apr 2025, 07:18
                    0
                    • K Offline
                      K Offline
                      Kevin Hoang
                      wrote on 3 Apr 2025, 03:32 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 3 Apr 2025, 07:18
                      0
                      • K Kevin Hoang
                        3 Apr 2025, 03:32

                        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 3 Apr 2025, 07:18 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
                          3 Apr 2025, 00:58

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

                          J Offline
                          J Offline
                          JonB
                          wrote on 3 Apr 2025, 07:18 last edited by JonB 4 Mar 2025, 07:19
                          #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 3 Apr 2025, 07:55
                          0
                          • J JonB
                            3 Apr 2025, 07:18

                            @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 3 Apr 2025, 07:55 last edited by
                            #16

                            @JonB Thanks for your advice.

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              Kevin Hoang
                              wrote on 3 Apr 2025, 14:20 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 7 Apr 2025, 02:19
                              0
                              • K Kevin Hoang
                                3 Apr 2025, 14:20

                                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 7 Apr 2025, 02:19 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

                                13/18

                                3 Apr 2025, 03:32

                                • Login

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