PDA

View Full Version : How to simulate QTouchEvent using mouse



dpatel
17th August 2010, 06:00
Hi,

I want to simulate QTouchEvent using Mouseevents. I mean I don't have a touch screen, but want to simulate touch behavior using Mouse. Is this possible and how?

Thanks
D

tbscope
17th August 2010, 06:05
When a mouse click event occures, eat it and send a touch event.

fabioferrario
21st March 2011, 15:23
I can easily eat the MouseEvent but have real hard times in posting the touch event.

bool MouseTouchFilter::eventFilter(QObject *obj, QEvent *event)
{
...
else if (event->type() == QEvent::MouseButtonPress){
QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
qDebug("Ate mousepress");
QTouchEvent *tev=new QTouchEvent(QEvent::TouchBegin,QTouchEvent::TouchS creen);
// QCoreApplication::sendEvent(obj,tev);
return QObject::eventFilter(obj, tev);
}

Forgot to ask for help...
Can you help me? I always get a nice SEGFAULT..after posting the TouchEvent.

Fabio

Added after 33 minutes:

Eventually this code seems to work as a general structure but fails in filtering the event

else if (event->type() == QEvent::MouseButtonPress){
QMouseEvent *mEvent = static_cast<QMouseEvent *>(event);
qDebug("Ate mousepress");
QTouchEvent *tEvent=new QTouchEvent(QEvent::TouchBegin,QTouchEvent::TouchS creen);
return QObject::eventFilter(obj, tEvent);
}

Once I catch the event on the object grabbing the mouse event i get back QEvent::MouseButtonPress instead of QEvent::TouchBegin, so say event 2 instead of 192.

imageWidget->setAttribute(Qt::WA_AcceptTouchEvents);

has been called just after istantiating the Widget.

thru
22nd March 2011, 09:48
Just a suggestion to try, haven't tested it myself. Try using
QCoreApplication::postEvent to allow completion of the processing of the mouseEvent.

fabioferrario
22nd March 2011, 11:01
Thanks ,

this is something I tried, as you see in the code I used SendEvent() and also tried postEvent().

The issue I have right now is the TouchEvent berin transformed in MouseEvent.
The Filter sends the TouchEvent and the Widget gets a MouseEvent.

I took care in the Widget to accept the TouchEvents but still no joy :(

this->setAttribute(Qt::WA_AcceptTouchEvents);
this->setAttribute(Qt::WA_NoMousePropagation);
this->setAttribute(Qt::WA_TouchPadAcceptSingleTouchEvent s);

dsonck92
28th April 2016, 01:40
Just for anyone that is still wondering how, you can use the QTest::touchEvent framework. I've implemented a custom touch simulation by catching the mouse events and generating the .press(), .move() and .release() events.

As per documentation, the events are sent when the object goes out of scope so I call it like:


QTest::touchEvent(widget).press(0,pos);
...
QTest::touchEvent(widget).move(0,pos);