How to print coordinates of fingers in Qt6 for QOpenGLWindow?
-
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'
and like this:
QApplication::setAttribute(Qt::WA_AcceptTouchEvents);
error: cannot initialize a parameter of type 'Qt::ApplicationAttribute' with an rvalue of type 'Qt::WidgetAttribute'
I tried to accept it in the window constructor but
QOpenGLWindow
doesn't have thesetAttribute
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
-
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
-
Hi,
WA is for Widget Attribute, QOpenGLWindow is QWindow.
You need to change to QOpenGLWidget.
-
Hi,
WA is for Widget Attribute, QOpenGLWindow is QWindow.
You need to change to QOpenGLWidget.
@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
hasWA_AcceptTouchEvents
. I think it is a bug of Qt Creator:I created the topic: Qt Creator assumes that WA_AcceptTouchEvents is in the Qt::ApplicationAttribute
-
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 likecounter = 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; } }
-
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
-
8 8Observer8 has marked this topic as solved on
-
@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
hasWA_AcceptTouchEvents
. I think it is a bug of Qt Creator:I created the topic: Qt Creator assumes that WA_AcceptTouchEvents is in the Qt::ApplicationAttribute
@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().
That's correct, I just wanted to give the correct class in the context of
WA_AcceptTouchEvents
.