<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QTouchEvent &#x2F; Mouse event interaction]]></title><description><![CDATA[<p dir="auto">I have a class derived from QGraphicsObject. I'd like users to be able to interact with this object with a mouse or a touchscreen ( if one is installed ). In some cases I need to toggle a state when either the mouse down or TouchBegin event is detected. The problem I'm having is that when the user touches the screen I get both a QEvent::TouchBegin and a mousePressEvent. Since my code needs to toggle the state of a variable in either of these cases I end up toggling twice. I've called event-&gt;accept() when I get any QEvent::Touch* events thinking that might block it from calling mousePressEvent but that doesn't appear to be the case. Is there a way to stop the mouse events from being called if I handle it through the touch events? I could set some internal state and block mouse events while handling touch events but that seems pretty sketchy.</p>
<p dir="auto">Here's my sample code which displays the problem. I'm running on Win7 64 if that matters.</p>
<p dir="auto">@#include &lt;QtGui&gt;</p>
<p dir="auto">class Test : public  QGraphicsObject<br />
{<br />
virtual QRectF boundingRect() const<br />
{<br />
return QRectF(0,0,800,800);<br />
}<br />
void mousePressEvent ( QGraphicsSceneMouseEvent * event )<br />
{<br />
qDebug() &lt;&lt; "Mouse button press";<br />
}<br />
void mouseMoveEvent ( QGraphicsSceneMouseEvent * event )<br />
{<br />
qDebug() &lt;&lt; "Mouse move";<br />
}<br />
virtual void paint(QPainter <em>painter, const QStyleOptionGraphicsItem <em>option, QWidget <em>widget = 0)<br />
{<br />
painter-&gt;setBackground(QColor(255,0,0));<br />
painter-&gt;fillRect(boundingRect(), QBrush(QColor(255,0,0)));<br />
painter-&gt;setPen(QColor(0,0,0));<br />
}<br />
bool sceneEvent(QEvent</em> event)<br />
{<br />
switch(event-&gt;type())<br />
{<br />
case QEvent::TouchBegin:<br />
case QEvent::TouchEnd:<br />
case QEvent::TouchUpdate:<br />
{<br />
event-&gt;accept();<br />
QTouchEvent</em> touchEvent = static_cast&lt;QTouchEvent</em>&gt;(event);<br />
if( touchEvent)<br />
{<br />
for( int ix = 0; ix &lt; touchEvent-&gt;touchPoints().size(); ++ix)<br />
{<br />
const QTouchEvent::TouchPoint&amp; p1 = touchEvent-&gt;touchPoints().at(ix);<br />
qDebug() &lt;&lt; "touch " &lt;&lt; event &lt;&lt; " is at " &lt;&lt; p1.scenePos().x() &lt;&lt; ", " &lt;&lt; p1.scenePos().y() &lt;&lt; " : " &lt;&lt; <a href="http://p1.id" target="_blank" rel="noopener noreferrer nofollow ugc">p1.id</a>();<br />
}<br />
}<br />
return true;<br />
}<br />
break;<br />
}<br />
return QGraphicsObject::sceneEvent(event);<br />
}</p>
<p dir="auto">public:<br />
Test()<br />
{<br />
this-&gt;setAcceptTouchEvents(true);<br />
}<br />
};</p>
<p dir="auto">int main(int argc, char <em>argv[])<br />
{<br />
QApplication a(argc, argv);<br />
QGraphicsScene</em> scene = new QGraphicsScene(0, 0, 800, 800);<br />
scene-&gt;addItem(new Test());<br />
QGraphicsView view(scene);<br />
view.show();<br />
return a.exec();<br />
}<br />
@</p>
]]></description><link>https://forum.qt.io/topic/4418/qtouchevent-mouse-event-interaction</link><generator>RSS for Node</generator><lastBuildDate>Sat, 14 Nov 2026 23:56:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/4418.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 16 Mar 2011 16:47:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QTouchEvent &#x2F; Mouse event interaction on Wed, 04 May 2011 15:37:56 GMT]]></title><description><![CDATA[<p dir="auto">Ok, the post may be outdated and you have solved this issue for sure, but when i read your post, i rememdered one thing i read in the "documenation of QTouchEvent":<a href="http://doc.qt.nokia.com/latest/qtouchevent.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.nokia.com/latest/qtouchevent.html</a>, which sounds like the solution for your problem.</p>
<p dir="auto"><em>Mouse Events and the Primary Touch Point</em></p>
<p dir="auto">QTouchEvent delivery is independent from that of QMouseEvent. On some windowing systems, mouse events are also sent for the primary touch point. This means it is possible for your widget to receive both QTouchEvent and QMouseEvent for the same user interaction point. You can use the QTouchEvent::TouchPoint::isPrimary() function to identify the primary touch point.</p>
<p dir="auto">Note that on some systems, it is possible to receive touch events without a primary touch point. All this means is that there will be no mouse event generated for the touch points in the QTouchEvent.</p>
]]></description><link>https://forum.qt.io/post/86397</link><guid isPermaLink="true">https://forum.qt.io/post/86397</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Wed, 04 May 2011 15:37:56 GMT</pubDate></item></channel></rss>