From bd9eb891709346853d7bf49b75094a3fd81e42e9 Mon Sep 17 00:00:00 2001 From: Marcus Sonestedt Date: Mon, 15 Apr 2024 11:04:54 +0200 Subject: [PATCH] Add multitouch support via qtouchevent --- .gitignore | 1 + tspress.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++-- tspress.h | 9 ++++++--- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 711955c..ed0ed44 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Makefile GeneratedFiles/ Objects/ *.user +build diff --git a/tspress.cpp b/tspress.cpp index 879ac0f..b31a4c8 100644 --- a/tspress.cpp +++ b/tspress.cpp @@ -35,12 +35,15 @@ #include #include +#include + #define MIN_WIDTH_FOR_BUTTONS 400 TsPress::TsPress(QWidget *parent) : QMainWindow(parent) { setWindowFlags(Qt::Window | Qt::CustomizeWindowHint); + setAttribute(Qt::WA_AcceptTouchEvents); m_down.setX(-1); m_up.setX(-1); @@ -62,7 +65,7 @@ void TsPress::onPressed(int btn) void TsPress::mousePressEvent(QMouseEvent *event) { - m_which->setText(QString("Down: %1, %2").arg(event->position().x()).arg(event->position().y())); + m_which->setText(QString("Down: %1, %2").arg(event->pos().x()).arg(event->pos().y())); m_down = event->pos(); m_up.setX(-1); update(); @@ -70,7 +73,7 @@ void TsPress::mousePressEvent(QMouseEvent *event) void TsPress::mouseReleaseEvent(QMouseEvent *event) { - m_which->setText(QString("Up: %1, %2").arg(event->position().x()).arg(event->position().y())); + m_which->setText(QString("Up: %1, %2").arg(event->pos().x()).arg(event->pos().y())); m_up = event->pos(); update(); } @@ -207,3 +210,43 @@ void TsPress::layoutWindow() setCentralWidget(widget); } +namespace { + +QString toString(Qt::TouchPointState state){ + switch (state) { + case Qt::TouchPointPressed: return "Pressed"; + case Qt::TouchPointMoved: return "Moved"; + case Qt::TouchPointStationary: return "Stationary"; + case Qt::TouchPointReleased: return "Released"; + default: return "???"; + } +} + +} + +bool TsPress::event(QEvent* event) +{ + QString type; + + switch (event->type()) { + case QEvent::TouchBegin: type ="Begin"; break; + case QEvent::TouchUpdate: type = "Update"; break; + case QEvent::TouchEnd: type ="End"; break; + default: return QMainWindow::event(event); + } + + const auto e = static_cast(event); + qInfo() << e << ":" << e->type() << e->device() << e->target() << e->window() << "time:" << e->timestamp() + << "\n points" << e->touchPoints() + << "\n states:" << e->touchPointStates() + << "\n modifiers" << e->modifiers(); + + auto str = QString("%1 %2 (%3 point(s))\n").arg(type, e->device()->name()).arg(e->touchPoints().count()); + + for (const auto &p: e->touchPoints()) { + str += QString("(%0,%1 %2)").arg(p.pos().x()).arg(p.pos().y()).arg(toString(p.state())); + } + m_which->setText(str); + return true; + +} diff --git a/tspress.h b/tspress.h index b30b210..e84837b 100644 --- a/tspress.h +++ b/tspress.h @@ -32,6 +32,8 @@ #ifndef TSPRESS_H #define TSPRESS_H +class QTouchEvent; + #include #include #include @@ -48,9 +50,10 @@ public slots: void onPressed(int btn); protected: - void mousePressEvent(QMouseEvent *); - void mouseReleaseEvent(QMouseEvent *); - void paintEvent(QPaintEvent *); + void mousePressEvent(QMouseEvent *) override; + void mouseReleaseEvent(QMouseEvent *) override; + void paintEvent(QPaintEvent *) override; + bool event(QEvent *) override; private: void layoutWindow();