<?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[How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?]]></title><description><![CDATA[<p dir="auto">I have a QGraphicsView which contains large number of QGraphicsItem. I want to check, mouse clicked point is inside any of QGraphicsItem or not. And if it is inside in any QGraphicsItem, that item should be highlighted. But though the mouse clicked point is not inside in any QGraphicsItem, some of the polylines are getting highlighted</p>
<pre><code>bool myViewer::eventFilter(QObject* watched, QEvent* event)
{
    bool filterEvent = false;

    switch (event-&gt;type()) 
   {
        case QEvent::MouseButtonPress: 
        {
             FindingPosition(event);
             break;
        }
   }
}

void myViewer::FindingPosition(QEvent* event)
{
     QMouseEvent* mouseEvent = static_cast&lt;QMouseEvent*&gt;(event);
     QPointF mousePoint = _view-&gt;mapToScene(mouseEvent-&gt;pos());
     foreach(QGraphicsItem* t , _scene-&gt;items())
     {
           if(t-&gt;contains(t-&gt;mapFromScene(mousePoint)))
           {
               QGraphicsRectItem* rItem = qgraphicsitem_cast&lt;QGraphicsRectItem*&gt;(t);
               if (rItem) 
               {
                   rItem-&gt;setSelected(false);
                   QPen mPen;
                   mPen.setWidth(1);
                   mPen.setBrush(Qt::red);
                   rItem-&gt;setPen(mPen);
                   break;
                 }
                 else
                 {
                     QGraphicsPathItem* pItem = qgraphicsitem_cast&lt;QGraphicsPathItem*&gt;(t);
                     if (pItem) 
                     {
                         pItem-&gt;setSelected(false);
                         QPen mPen;
                         mPen.setWidth(1);
                         mPen.setBrush(Qt::red);
                         pItem-&gt;setPen(mPen);
                         break;
                     }
                 }
             }
         }
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/136792/how-to-determine-mouse-clicked-point-is-inside-in-any-qgraphicsitem-from-scene-in-qt</link><generator>RSS for Node</generator><lastBuildDate>Sat, 09 May 2026 05:03:44 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/136792.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 26 May 2022 14:21:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt? on Thu, 26 May 2022 18:19:03 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Did you make the items <a href="https://doc.qt.io/qt-5/qgraphicsitem.html#GraphicsItemFlag-enum" target="_blank" rel="noopener noreferrer nofollow ugc">selectable</a> ?</p>
]]></description><link>https://forum.qt.io/post/715475</link><guid isPermaLink="true">https://forum.qt.io/post/715475</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 26 May 2022 18:19:03 GMT</pubDate></item><item><title><![CDATA[Reply to How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt? on Thu, 26 May 2022 15:24:45 GMT]]></title><description><![CDATA[<p dir="auto">Check the mapping of mouse position to the scene is right or not. You know the exact positions and rects of all items and debug into the code FindingPosition(QEvent* event) and stop at the wrong selection of the item. Manually check the mouse position and item geometry. You will be able to find the issue quickly. Do not look at the code only.</p>
<p dir="auto">I had a quick look at what I have. I used customized classes to inherit QGraphicsItem and override<br />
protected:<br />
void mousePressEvent( QGraphicsSceneMouseEvent * event ) override;<br />
In this way it may be easier for you to know which one to highlight.</p>
]]></description><link>https://forum.qt.io/post/715463</link><guid isPermaLink="true">https://forum.qt.io/post/715463</guid><dc:creator><![CDATA[JoeCFD]]></dc:creator><pubDate>Thu, 26 May 2022 15:24:45 GMT</pubDate></item><item><title><![CDATA[Reply to How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt? on Thu, 26 May 2022 14:58:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tushu">@<bdi>tushu</bdi></a> said in <a href="/post/715455">How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?</a>:</p>
<blockquote>
<p dir="auto">foreach(QGraphicsItem* t , _scene-&gt;items())</p>
</blockquote>
<p dir="auto">Qt Graphics is designed so that a <em>much</em> faster way of doing this is <a href="https://doc.qt.io/qt-5/qgraphicsview.html#items-1" target="_blank" rel="noopener noreferrer nofollow ugc">QList&lt;QGraphicsItem *&gt; QGraphicsView::items(const QPoint &amp;pos) const</a> (or possibly <a href="https://doc.qt.io/qt-5/qgraphicsscene.html#items-1" target="_blank" rel="noopener noreferrer nofollow ugc">QList&lt;QGraphicsItem *&gt; QGraphicsScene::items(const QPointF &amp;pos, ...)</a>).</p>
<p dir="auto">I know nothing about your polylines or highlighting.</p>
]]></description><link>https://forum.qt.io/post/715460</link><guid isPermaLink="true">https://forum.qt.io/post/715460</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 26 May 2022 14:58:52 GMT</pubDate></item><item><title><![CDATA[Reply to How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt? on Thu, 26 May 2022 14:53:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/jsulm">@<bdi>jsulm</bdi></a>  Can anyone help me ?</p>
]]></description><link>https://forum.qt.io/post/715458</link><guid isPermaLink="true">https://forum.qt.io/post/715458</guid><dc:creator><![CDATA[tushu]]></dc:creator><pubDate>Thu, 26 May 2022 14:53:54 GMT</pubDate></item></channel></rss>