Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. QtonPi
  4. How to make widgets react to touches instead of mouse clicks
Forum Updated to NodeBB v4.3 + New Features

How to make widgets react to touches instead of mouse clicks

Scheduled Pinned Locked Moved Solved QtonPi
7 Posts 2 Posters 3.4k Views 2 Watching
  • 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.
  • D Offline
    D Offline
    DL5EU
    wrote on 4 Jun 2020, 19:20 last edited by
    #1

    Dear Qt community,

    I am new to Qt and would like to develop a control device based on a Raspberry Pi and an LCD with a touchscreen.

    What I have done so far and what works:

    • Set up a cross-compilation environment with Qt 5.15.0 and Qt Creator 4.12.1;
    • Set up a Raspberry Pi 4B with Raspberry OS Lite (no Desktop);
    • Cross-compilation works, I can run and remote debug a program that runs on the Pi in fullscreen mode (EGLFS), no problem so far;
    • For the touchscreen, tslib (libts0) is used. ts_calibrate and ts_test work;
    • When I connect a keyboard and mouse to the Pi, I can use the program.

    However, a physical keyboard is not needed and I would like to use the touchscreen instead of a mouse (most of the widgets are QPushButtons).

    Touches are translated into mouse events. When I override the mousePressEvent method of the MainWindow, this method is called whenever I touch the screen. So far, so good. However, when I try to "click" a QPushButton by "touching" it, nothing happens.

    What I would like to achieve is that the widgets recognise touches as mouse clicks, i.e. that they behave as when the left mouse button was clicked. How can I do that?

    I have also read that I would need to handle the QMouseEvents that are generated when I touch the screen in the widgets. Does this mean that I have to subclass all widgets that I would like to use (QPushButton, ...) and override the mousePressedEvent of every new widget class or is there a more elegant way? I think that the solution must be either very easy or complicated :-)

    Thank you very much for your help,

    Ralf

    S 1 Reply Last reply 5 Jun 2020, 05:40
    0
    • D DL5EU
      4 Jun 2020, 19:20

      Dear Qt community,

      I am new to Qt and would like to develop a control device based on a Raspberry Pi and an LCD with a touchscreen.

      What I have done so far and what works:

      • Set up a cross-compilation environment with Qt 5.15.0 and Qt Creator 4.12.1;
      • Set up a Raspberry Pi 4B with Raspberry OS Lite (no Desktop);
      • Cross-compilation works, I can run and remote debug a program that runs on the Pi in fullscreen mode (EGLFS), no problem so far;
      • For the touchscreen, tslib (libts0) is used. ts_calibrate and ts_test work;
      • When I connect a keyboard and mouse to the Pi, I can use the program.

      However, a physical keyboard is not needed and I would like to use the touchscreen instead of a mouse (most of the widgets are QPushButtons).

      Touches are translated into mouse events. When I override the mousePressEvent method of the MainWindow, this method is called whenever I touch the screen. So far, so good. However, when I try to "click" a QPushButton by "touching" it, nothing happens.

      What I would like to achieve is that the widgets recognise touches as mouse clicks, i.e. that they behave as when the left mouse button was clicked. How can I do that?

      I have also read that I would need to handle the QMouseEvents that are generated when I touch the screen in the widgets. Does this mean that I have to subclass all widgets that I would like to use (QPushButton, ...) and override the mousePressedEvent of every new widget class or is there a more elegant way? I think that the solution must be either very easy or complicated :-)

      Thank you very much for your help,

      Ralf

      S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 5 Jun 2020, 05:40 last edited by
      #2

      @DL5EU said in How to make widgets react to touches instead of mouse clicks:

      Touches are translated into mouse events. When I override the mousePressEvent method of the MainWindow, this method is called whenever I touch the screen. So far, so good. However, when I try to "click" a QPushButton by "touching" it, nothing happens.

      If you still have that even handler in MainWindow, it is "eating" events that would normally go to your push buttons. Either remove that code or at least set accepted to false:

      event->setAccepted(false);
      

      (Z(:^

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DL5EU
        wrote on 5 Jun 2020, 08:14 last edited by
        #3

        Hello @sierdzio,

        I have added this code in MainWindow only for testing after I had discovered that nothing happened when I "touched" a PushButton and then deleted it again.

        I have now created a descendant of QPushButton and added code to show the reaction to mousePressEvents. Then I have added such a button to my MainWindow. When I click the button with the mouse it shows the events but when I "touch" it, nothing happens (as before).

        Is this a normal behaviour or am I doing something wrong? I cannot imagine that I am the first one who is trying to do something like this.

        Could it be that Qt does not correctly recognise the touch screen coordinates even if ts_test and ts_calibrate work as expected?

        Kind regards,

        Ralf

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 5 Jun 2020, 08:30 last edited by
          #4

          I don't know, sorry. Usually, touch events are treated like mouse clicks and there is no further tinkering necessary. So yes, maybe Qt is not getting the touch events properly... I don't know.

          (Z(:^

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DL5EU
            wrote on 5 Jun 2020, 13:46 last edited by
            #5

            Hi,

            here are some news. Perhaps somebody can help.

            As far as I can tell up to now, the coordinates of the touch screen are not correctly detected or mapped or whatever.

            The problem is that the X and Y coordinates that I get in the mouse events that are generated when I touch the screen do not correspond to the coordinates in the events that are generated when I use the mouse. When I touch the screen at a position where the X and Y coordinates correspond to the position of the QPushButton it reacts as expected. So, the reason for the problem is clear.

            Any idea how to correct it? Please don't forget: the display is ok and ts_calibrate and ts_test work as expected (with rotation = 0). For information, on my Raspberry Pi 4 I use the OpenGL driver with fake KMS (EGLFS).

            Thank you very much for your help,

            Ralf

            1 Reply Last reply
            1
            • D Offline
              D Offline
              DL5EU
              wrote on 7 Jun 2020, 08:54 last edited by
              #6

              Hello,

              I managed to solve the problem. The touch screen was not correctly recognised.

              This combination solves the problem:

              QT_QPA_EGLFS_NO_LIBINPUT=1
              QT_QPA_EGLFS_TSLIB=1

              Probably the only combination that I had not tried :-) Apparently TSLIB has to be explicitly activated if LIBINPUT is deactivated.

              Kind regards,

              Ralf

              1 Reply Last reply
              2
              • S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 7 Jun 2020, 09:51 last edited by
                #7

                Great, thanks for sharing the solution!

                (Z(:^

                1 Reply Last reply
                0

                1/7

                4 Jun 2020, 19:20

                • Login

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