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. How to print coordinates of fingers in Qt6 for QOpenGLWindow?

How to print coordinates of fingers in Qt6 for QOpenGLWindow?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 2 Posters 795 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.
  • 8Observer88 Offline
    8Observer88 Offline
    8Observer8
    wrote on last edited by 8Observer8
    #1

    Hello,

    I tried to accept touch events in Qt6 like this, in main.cpp:

    QApplication::setAttribute(Qt::ApplicationAttribute::WA_AcceptTouchEvents);
    

    error: no member named 'WA_AcceptTouchEvents' in 'Qt::ApplicationAttribute'

    f62b0b71-cf5f-4b69-b058-de927f899159-image.png

    and like this:

    QApplication::setAttribute(Qt::WA_AcceptTouchEvents);
    

    error: cannot initialize a parameter of type 'Qt::ApplicationAttribute' with an rvalue of type 'Qt::WidgetAttribute'

    166a6896-5f89-46fa-8c17-90d6cda654cf-image.png

    I tried to accept it in the window constructor but QOpenGLWindow doesn't have the setAttribute method:

    error: use of undeclared identifier 'setAttribute'; did you mean 'QInputMethodEvent::Attribute'?

    OpenGLWindow::OpenGLWindow()
    {
        setTitle("OpenGL ES 2.0, Qt6, C++");
        resize(350, 350);
        setAttribute(Qt::WA_AcceptTouchEvents);
    }
    

    My pro-file:

    QT += core gui opengl widgets
    
    CONFIG += c++17
    
    # You can make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # disables all the APIs deprecated before Qt 6.0.0
    # DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
    
    SOURCES += \
        main.cpp \
        opengl_window.cpp
    
    HEADERS += \
        opengl_window.h
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    Cross-ref: https://stackoverflow.com/questions/78087509/how-to-accept-touch-events-in-qt6

    1 Reply Last reply
    0
    • 8Observer88 Offline
      8Observer88 Offline
      8Observer8
      wrote on last edited by 8Observer8
      #5

      Solution:

      opengl_window.h

      #ifndef OPENGL_WINDOW_H
      #define OPENGL_WINDOW_H
      
      #include <QtGui/QTouchEvent>
      #include <QtOpenGL/QOpenGLWindow>
      
      class OpenGLWindow : public QOpenGLWindow
      {
          Q_OBJECT
      
      public:
          OpenGLWindow();
      
      private:
          void touchEvent(QTouchEvent *event) override;
          int m_counter = 0;
      };
      
      #endif // OPENGL_WINDOW_H
      

      opengl_window.cpp

      #include "opengl_window.h"
      
      OpenGLWindow::OpenGLWindow()
      {
          setTitle("OpenGL ES 2.0, Qt6, C++");
          resize(350, 350);
      }
      
      void OpenGLWindow::touchEvent(QTouchEvent *event)
      {
          switch (event->type())
          {
              case QEvent::TouchBegin:
                  qDebug() << "touch begin";
                  event->accept();
                  break;
              case QEvent::TouchEnd:
                  qDebug() << "touch end";
                  break;
              case QEvent::TouchUpdate:
                  qDebug() << "counter = " << m_counter;
                  m_counter++;
                  for (int i = 0; i < event->points().length(); i++) {
                      int x = event->points()[i].position().x();
                      int y = event->points()[i].position().y();
                      qDebug() << x << " " << y;
                  }
                  break;
          }
      }
      

      main.cpp

      #include <QtWidgets/QApplication>
      #include "opengl_window.h"
      
      int main(int argc, char *argv[])
      {
          QApplication::setAttribute(Qt::ApplicationAttribute::AA_UseDesktopOpenGL);
          QApplication a(argc, argv);
          OpenGLWindow w;
          w.show();
          return a.exec();
      }
      

      pro:

      QT       += core gui opengl widgets
      
      CONFIG += c++17
      
      # You can make your code fail to compile if it uses deprecated APIs.
      # In order to do so, uncomment the following line.
      # disables all the APIs deprecated before Qt 6.0.0
      DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
      
      SOURCES += \
          main.cpp \
          opengl_window.cpp
      
      HEADERS += \
          opengl_window.h
      
      # Default rules for deployment.
      qnx: target.path = /tmp/$${TARGET}/bin
      else: unix:!android: target.path = /opt/$${TARGET}/bin
      !isEmpty(target.path): INSTALLS += target
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        WA is for Widget Attribute, QOpenGLWindow is QWindow.

        You need to change to QOpenGLWidget.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        8Observer88 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          WA is for Widget Attribute, QOpenGLWindow is QWindow.

          You need to change to QOpenGLWidget.

          8Observer88 Offline
          8Observer88 Offline
          8Observer8
          wrote on last edited by 8Observer8
          #3

          @SGaist said in How to accept touch events in Qt6?:

          You need to change to QOpenGLWidget.

          No, I don't need because:

          Unlike widgets, QWindows receive touch events always, there is no need to opt in. When working directly with a QWindow, it is enough to reimplement QWindow::touchEvent().

          https://doc.qt.io/qt-6/qtouchevent.html#event-handling

          It was answer from musicamante: https://stackoverflow.com/questions/78087509/how-to-accept-touch-events-in-qt6?noredirect=1#comment137666237_78087509

          I don't like QOpenGLWidget because it cannot make a smooth animation with QTimer. QOpenGLWindow has the frameSwapped signal:

              QSurfaceFormat surfaceFormat;
              surfaceFormat.setSwapInterval(1);
              connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
              setFormat(surfaceFormat);
          

          But the Qt Creator's autocompletion systems shows that Qt::ApplicationAttribute has WA_AcceptTouchEvents. I think it is a bug of Qt Creator:

          25b9a54f-1888-4a55-9595-52a2168ad5ac-image.png

          I created the topic: Qt Creator assumes that WA_AcceptTouchEvents is in the Qt::ApplicationAttribute

          SGaistS 1 Reply Last reply
          0
          • 8Observer88 Offline
            8Observer88 Offline
            8Observer8
            wrote on last edited by 8Observer8
            #4

            But when I run the next code on the real device, touch a screen (it prints counter = 0), holding a finger on the screen, try to touch with another finger and doesn't print the next line like counter = 1. It seams like multi touch is disabled.

            void OpenGLWindow::touchEvent(QTouchEvent *event)
            {
                switch (event->type())
                {
                    case QEvent::TouchBegin:
                        qDebug() << "counter = " << m_counter;
                        m_counter++;
                        break;
                }
            }
            
            1 Reply Last reply
            0
            • 8Observer88 Offline
              8Observer88 Offline
              8Observer8
              wrote on last edited by 8Observer8
              #5

              Solution:

              opengl_window.h

              #ifndef OPENGL_WINDOW_H
              #define OPENGL_WINDOW_H
              
              #include <QtGui/QTouchEvent>
              #include <QtOpenGL/QOpenGLWindow>
              
              class OpenGLWindow : public QOpenGLWindow
              {
                  Q_OBJECT
              
              public:
                  OpenGLWindow();
              
              private:
                  void touchEvent(QTouchEvent *event) override;
                  int m_counter = 0;
              };
              
              #endif // OPENGL_WINDOW_H
              

              opengl_window.cpp

              #include "opengl_window.h"
              
              OpenGLWindow::OpenGLWindow()
              {
                  setTitle("OpenGL ES 2.0, Qt6, C++");
                  resize(350, 350);
              }
              
              void OpenGLWindow::touchEvent(QTouchEvent *event)
              {
                  switch (event->type())
                  {
                      case QEvent::TouchBegin:
                          qDebug() << "touch begin";
                          event->accept();
                          break;
                      case QEvent::TouchEnd:
                          qDebug() << "touch end";
                          break;
                      case QEvent::TouchUpdate:
                          qDebug() << "counter = " << m_counter;
                          m_counter++;
                          for (int i = 0; i < event->points().length(); i++) {
                              int x = event->points()[i].position().x();
                              int y = event->points()[i].position().y();
                              qDebug() << x << " " << y;
                          }
                          break;
                  }
              }
              

              main.cpp

              #include <QtWidgets/QApplication>
              #include "opengl_window.h"
              
              int main(int argc, char *argv[])
              {
                  QApplication::setAttribute(Qt::ApplicationAttribute::AA_UseDesktopOpenGL);
                  QApplication a(argc, argv);
                  OpenGLWindow w;
                  w.show();
                  return a.exec();
              }
              

              pro:

              QT       += core gui opengl widgets
              
              CONFIG += c++17
              
              # You can make your code fail to compile if it uses deprecated APIs.
              # In order to do so, uncomment the following line.
              # disables all the APIs deprecated before Qt 6.0.0
              DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
              
              SOURCES += \
                  main.cpp \
                  opengl_window.cpp
              
              HEADERS += \
                  opengl_window.h
              
              # Default rules for deployment.
              qnx: target.path = /tmp/$${TARGET}/bin
              else: unix:!android: target.path = /opt/$${TARGET}/bin
              !isEmpty(target.path): INSTALLS += target
              
              1 Reply Last reply
              0
              • 8Observer88 8Observer8 has marked this topic as solved on
              • 8Observer88 8Observer8

                @SGaist said in How to accept touch events in Qt6?:

                You need to change to QOpenGLWidget.

                No, I don't need because:

                Unlike widgets, QWindows receive touch events always, there is no need to opt in. When working directly with a QWindow, it is enough to reimplement QWindow::touchEvent().

                https://doc.qt.io/qt-6/qtouchevent.html#event-handling

                It was answer from musicamante: https://stackoverflow.com/questions/78087509/how-to-accept-touch-events-in-qt6?noredirect=1#comment137666237_78087509

                I don't like QOpenGLWidget because it cannot make a smooth animation with QTimer. QOpenGLWindow has the frameSwapped signal:

                    QSurfaceFormat surfaceFormat;
                    surfaceFormat.setSwapInterval(1);
                    connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
                    setFormat(surfaceFormat);
                

                But the Qt Creator's autocompletion systems shows that Qt::ApplicationAttribute has WA_AcceptTouchEvents. I think it is a bug of Qt Creator:

                25b9a54f-1888-4a55-9595-52a2168ad5ac-image.png

                I created the topic: Qt Creator assumes that WA_AcceptTouchEvents is in the Qt::ApplicationAttribute

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @8Observer8 said in How to print coordinates of fingers in Qt6 for QOpenGLWindow?:

                @SGaist said in How to accept touch events in Qt6?:

                You need to change to QOpenGLWidget.

                No, I don't need because:

                Unlike widgets, QWindows receive touch events always, there is no need to opt in. When working directly with a QWindow, it is enough to reimplement QWindow::touchEvent().

                https://doc.qt.io/qt-6/qtouchevent.html#event-handling

                That's correct, I just wanted to give the correct class in the context of WA_AcceptTouchEvents.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                • Login

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