Unsolved How to use TouchPoint class present in QTouchEvent class
-
Hi,
I have a windows tablet and i am trying to display position of my touch. I am using QT CreatorI choose an example(Mouse move) from this link "http://www.codeprogress.com/cpp/libraries/qt/qtCaptureMouseMove.php"
I slightly modified the code to incorporate touch events
#include<QWindow>
#include<QTouchEvent>
#include <QApplication>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QLabel>class myLabel : public QLabel , QWindow
{
myLabel() {} //constructor copied from example
~myLabel() {} //copiedvoid touchEvent(QTouchEvent *event) { this->setText("X:"+QString::number(event->TouchPoint.lastPos().x())+"-- Y:"+QString::number(event->TouchPoint.lastPos().y())); }
}
Errors:
error: C2273: 'function-style cast': illegal as right side of '->' operator
error: C2228: left of '.lastPos' must have class/struct/union
error: C2228: left of '.x' must have class/struct/unionI am trying to create a simple touch based example. I am not
"class myLabel : public QLabel , QWindow"
is the way to get the touch events.If there is any simpler example online kindly let me know.
-
@BhanuKiranChaluvadi TouchPoint is a type not a variable!
Please check documentation http://doc.qt.io/qt-5/qtouchevent.html
You need to call http://doc.qt.io/qt-5/qtouchevent.html#touchPoints
It returns a list of touch points:QList<QTouchEvent::TouchPoint>
Then you can iterate over all points and print them out (a touch event can contain more than one event - multitouch):
QList<QTouchEvent::TouchPoint> points = event->touchPoints(); for (const QTouchEvent::TouchPoint& point : points) { ... }
-
@jsulm Thank you
-
I am sharing the code. might be help someone
I have 3 Files:
mainWinodw.h
mainWindow.cpp
main.cpp/////////////////////////////////////// mainWinodw.h /////////////////////////////////////////////////////////////
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QEvent>namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool event(QEvent *e)
{
qInfo("Event Entered");
switch(e->type())
{
case QEvent::TouchBegin:
qInfo("TouchBegin Event occured");
return true;
case QEvent::TouchUpdate:
qInfo("TouchUpdate Event occured");
return true;
case QEvent::TouchEnd:
qInfo("TouchEnd Event occured");
return true;
default:
qInfo("No touch Event occured");
return QWidget::event(e);
}
}private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////mainWinodw.cpp ////////////////////////////////////////////////////////////////////
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAttribute(Qt::WA_AcceptTouchEvents, true);
}MainWindow::~MainWindow()
{
delete ui;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////main.cpp /////////////////////////////////////////////////////////////////////////
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();return a.exec();
}
//////////////////////////////////////////////////////////////////////////////