Qt 6.11 is out! See what's new in the release
blog
Counter of touches
-
Hello. I try to make a simple example that shows a counter of touches:

I set this attribute:
setAttribute(Qt::WA_AcceptTouchEvents);I built the APK file and ran it on Android but the counter of touches isn't changed:
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QtGui/QTouchEvent> #include <QtWidgets/QLabel> #include <QtWidgets/QWidget> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); protected: bool event(QEvent *event) override; private: QLabel *m_counterOfTouchesLabel; int m_counterOfTouches = 0; }; #endif // WIDGET_Hwidget.cpp
#include "widget.h" #include <QtWidgets/QHBoxLayout> Widget::Widget(QWidget *parent) : QWidget(parent) { setWindowTitle("Touch Coords"); resize(300, 300); setAttribute(Qt::WA_AcceptTouchEvents); QHBoxLayout *hbox = new QHBoxLayout(this); m_counterOfTouchesLabel = new QLabel("Counter of touches: 0"); hbox->addWidget(m_counterOfTouchesLabel); setLayout(hbox); } Widget::~Widget() { } bool Widget::event(QEvent *event) { switch (event->type()) { case QEvent::TouchBegin: m_counterOfTouches++; m_counterOfTouchesLabel->setText("Counter of touches:" + QString::number(m_counterOfTouches)); break; default: break; } return true; } -
Solution:
bool Widget::event(QEvent *event) { if (event->type() == QEvent::TouchBegin) { m_counterOfTouches++; m_counterOfTouchesLabel->setText("Counter of touches: " + QString::number(m_counterOfTouches)); return true; } return QWidget::event(event); }
-
8 8Observer8 has marked this topic as solved on