<?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 do you handle selection of QGraphicsItem?]]></title><description><![CDATA[<p dir="auto">I've these in <code>hover</code> and <code>press</code> events in the subclass of <code>QGraphicsItem</code>:</p>
<pre><code>void Slice::hoverEnterEvent(QGraphicsSceneHoverEvent*){
    qDebug() &lt;&lt; "hoverEnterEvent" &lt;&lt; isSelected();
    if(isSelected()) return;
    m_color = Qt::gray;
    moveBy(dx, dy);
    auto view = static_cast&lt;PieView*&gt;(scene()-&gt;parent());
    emit view-&gt;mouseOver(m_series);

}
void Slice::hoverLeaveEvent(QGraphicsSceneHoverEvent*){
    qDebug() &lt;&lt; "hoverLeaveEvent" &lt;&lt; isSelected();
    if(isSelected()) return;
    m_color = m_realColor;
    moveBy(-dx, -dy);
    auto view = static_cast&lt;PieView*&gt;(scene()-&gt;parent());
    emit view-&gt;mouseLeave();
}
void Slice::mousePressEvent(QGraphicsSceneMouseEvent *e){
    if(e-&gt;button() != Qt::LeftButton) return;
    if(isSelected()) {
        setSelected(false);
        qDebug() &lt;&lt; "deselected" &lt;&lt; scene()-&gt;selectedItems().size();
    }
    else {
        setSelected(true);
        qDebug() &lt;&lt; "selected" &lt;&lt; scene()-&gt;selectedItems().size();
    }
}
</code></pre>
<p dir="auto">and here're the things I've done:</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/4db4e76a-d7bf-4854-b8b1-1287a12a5cfc.gif" alt="x1.gif" class=" img-fluid img-markdown" /></p>
<p dir="auto"><strong>first sequence:</strong> mouse entered, pressed the button, released the button and left the item. In the output, I got:</p>
<pre><code>hoverEnterEvent false
selected 1
hoverLeaveEvent true
</code></pre>
<p dir="auto">Correct.</p>
<p dir="auto"><strong>second sequence:</strong> same as (1) and I got these:</p>
<pre><code>hoverEnterEvent true
deselected 0
hoverLeaveEvent true
</code></pre>
<p dir="auto">Incorrect!</p>
<p dir="auto"><strong>third sequence:</strong> mouse entered and left the item, without any press, and got these:</p>
<pre><code>hoverEnterEvent true
hoverLeaveEvent true
</code></pre>
<p dir="auto">Incorrect, again!</p>
]]></description><link>https://forum.qt.io/topic/131942/how-do-you-handle-selection-of-qgraphicsitem</link><generator>RSS for Node</generator><lastBuildDate>Sun, 25 Oct 2026 19:13:21 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/131942.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 12 Nov 2021 12:56:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Sat, 13 Nov 2021 15:37:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a>, at that time I didn't have the call setSceneRect(....). With that line, now it doesn't move.</p>
]]></description><link>https://forum.qt.io/post/689914</link><guid isPermaLink="true">https://forum.qt.io/post/689914</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Sat, 13 Nov 2021 15:37:51 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Sat, 13 Nov 2021 15:35:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/emon-haque">@<bdi>Emon-Haque</bdi></a> said in <a href="/post/689871">How do you handle selection of QGraphicsItem?</a>:</p>
<blockquote>
<p dir="auto">when I first hover over a slice, rest of the slices are displaced. In the animation see, when mouse entered the bottom right slice for the first time, other two slices moved to the left! This happens only once with 3 slices. Why does it move?</p>
</blockquote>
<p dir="auto">This seem to be a miscalculation of your slice coordinates.<br />
When you hover the slice, it moves a bit out of the pie chart and the rest seems to follow. So the whole pie moves by X to the right.<br />
Can't tell where exactly but I would check the coordinates where the pie is drawn and how the relation between the "highlighted" (moved out) slice and the rest of the pie slices works.</p>
<p dir="auto">Edit:</p>
<p dir="auto">Or the whole pie "jumps" to the right in your scene to react on the geometry change... You need to test it. Do you center your pie in your scene?</p>
]]></description><link>https://forum.qt.io/post/689912</link><guid isPermaLink="true">https://forum.qt.io/post/689912</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Sat, 13 Nov 2021 15:35:59 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Sat, 13 Nov 2021 00:09:03 GMT]]></title><description><![CDATA[<p dir="auto">Problem is the base calls. Probably, there's something in the base <code>mouseReleaseEvent</code> that created the issue. I've taken it back to the original state, removed base constructor, update and base::...Event calls and added an empty mouseReleaseEvent like this:</p>
<pre><code>void Slice::mousePressEvent(QGraphicsSceneMouseEvent *e){
    if(e-&gt;button() != Qt::LeftButton) return;
    if(isSelected()) setSelected(false);
    else setSelected(true);
}
void Slice::mouseReleaseEvent(QGraphicsSceneMouseEvent*){}
</code></pre>
<p dir="auto">Now it behaves normally:</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/af24d45c-6e94-41db-bfb6-998322d33f73.gif" alt="x2.gif" class=" img-fluid img-markdown" /></p>
<p dir="auto">One thing that I couldn't figure out yet is: when I first hover over a slice, rest of the slices are displaced. In the animation see, when mouse entered the bottom right slice for the first time, other two slices moved to the left! This happens only once with 3 slices. Why does it move?</p>
<p dir="auto">Updated the github repo with these changes.</p>
]]></description><link>https://forum.qt.io/post/689871</link><guid isPermaLink="true">https://forum.qt.io/post/689871</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Sat, 13 Nov 2021 00:09:03 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 14:57:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a>, no, it doesn't use any group. In the PieView, <code>QGriphicsView</code>, constructor I initialize the <code>ellipse</code> and <code>text</code> AND PieView has a function <code>makePie</code> where I add slice. Here's the constructor:</p>
<pre><code>PieView::PieView(QWidget *parent) : QGraphicsView(parent){
    auto scene = new QGraphicsScene(this);
    setScene(scene);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ellipse = new QGraphicsEllipseItem();
    ellipse-&gt;setBrush(Qt::white);
    ellipse-&gt;setPen(Qt::NoPen);
    ellipse-&gt;setAcceptHoverEvents(false);
    text = new QGraphicsTextItem(ellipse);
    text-&gt;setAcceptHoverEvents(false);
    connect(this, &amp;PieView::mouseOver, this, &amp;PieView::onMouseOver);
    connect(this, &amp;PieView::mouseLeave, this, &amp;PieView::onMouseLeave);
}
</code></pre>
<p dir="auto">and here's makePie:</p>
<pre><code>void PieView::makePie(QVector&lt;PieSeries&gt;&amp; series){
    scene()-&gt;clear();
    m_total = 0;
    float startAngle = 0, spanAngle;
    for (int i = 0; i &lt; series.size(); i++) m_total += series[i].value;
    QRandomGenerator rand;
    auto rect = QRectF(0,0, 200,200);
    for (int i = 0; i &lt; series.size(); i++){
        spanAngle = series[i].value / m_total * 360;
        auto color = QColor::fromRgb(rand.global()-&gt;bounded(0,255), rand.global()-&gt;bounded(0,255), rand.global()-&gt;bounded(0,255));
        auto slice = new Slice(startAngle, spanAngle, color, rect, series[i]);
        scene()-&gt;addItem(slice);
        startAngle += spanAngle;
    }
    auto eRect = QRectF(QPoint(sceneRect().center().x() - 75, sceneRect().center().y() - 75), QSize(150,150));
    ellipse-&gt;setRect(eRect);
    text-&gt;setTextWidth(rect.width());
}
</code></pre>
]]></description><link>https://forum.qt.io/post/689835</link><guid isPermaLink="true">https://forum.qt.io/post/689835</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 12 Nov 2021 14:57:08 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 14:52:36 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/emon-haque">@<bdi>Emon-Haque</bdi></a><br />
Just one quickie: that pie chart/view does not use any <code>QGraphicsItemGroup</code> does it?</p>
]]></description><link>https://forum.qt.io/post/689832</link><guid isPermaLink="true">https://forum.qt.io/post/689832</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 12 Nov 2021 14:52:36 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 14:47:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a>, in <code>mouseOver</code> I add an ellipse (<code>ellipse = new QGraphicsEllipseItem();</code>) in the scene and that ellipse has a text (<code>text = new QGraphicsTextItem(ellipse);</code>):</p>
<pre><code>void PieView::onMouseOver(PieSeries &amp;s){
    text-&gt;document()-&gt;clear();
    auto cursor = QTextCursor(text-&gt;document());
    QTextBlockFormat blockFormat;
    QTextCharFormat charFormat;

    blockFormat.setAlignment(Qt::AlignHCenter);
    charFormat.setForeground(Qt::darkGreen);
    charFormat.setFontPointSize(16);
    cursor.setBlockFormat(blockFormat);
    cursor.setBlockCharFormat(charFormat);
    cursor.insertText(s.name);

    charFormat.setForeground(Qt::red);
    charFormat.setFontPointSize(14);
    cursor.insertBlock(blockFormat, charFormat);
    cursor.insertText(QString::number(s.value));

    charFormat.setForeground(Qt::black);
    charFormat.setFontPointSize(12);
    cursor.insertBlock(blockFormat, charFormat);
    cursor.insertText(QString::number(s.value / m_total * 100) + "%");

    text-&gt;setY(ellipse-&gt;boundingRect().center().y() - text-&gt;boundingRect().height()/2);
    scene()-&gt;addItem(ellipse);
}
</code></pre>
<p dir="auto">and I remove that onMouseLeave: <code>void PieView::onMouseLeave(){ scene()-&gt;removeItem(ellipse); }</code>. PieView is the subclass of <code>QGraphicsView</code>.</p>
<p dir="auto"><strong>EDIT</strong><br />
Will change that static to qobject cast later. Thanks for the reminder.</p>
]]></description><link>https://forum.qt.io/post/689828</link><guid isPermaLink="true">https://forum.qt.io/post/689828</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 12 Nov 2021 14:47:51 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 14:39:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/emon-haque">@<bdi>Emon-Haque</bdi></a></p>
<p dir="auto">I will try later with a minimal example to test the hover behavior.<br />
Usually it should work, unless you have done something else wrong.</p>
<p dir="auto">Does <code>mouseOver</code> do something with you item?<br />
Better use <code>qobject_cast</code> to cast  <code>QObjects</code>.</p>
<blockquote>
<p dir="auto">auto view = static_cast&lt;PieView*&gt;(scene()-&gt;parent());<br />
emit view-&gt;mouseOver(m_series);</p>
</blockquote>
]]></description><link>https://forum.qt.io/post/689825</link><guid isPermaLink="true">https://forum.qt.io/post/689825</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 12 Nov 2021 14:39:58 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 14:21:48 GMT]]></title><description><![CDATA[<p dir="auto">You can download the project from <a href="https://github.com/EmonHaque/QtPieChartTest" target="_blank" rel="noopener noreferrer nofollow ugc">GitHub</a> to avoid copy/paste and refactoring.</p>
]]></description><link>https://forum.qt.io/post/689816</link><guid isPermaLink="true">https://forum.qt.io/post/689816</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 12 Nov 2021 14:21:48 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 13:28:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a>, with <code>QGraphicsItem(parent)</code> in the constructor, added <code>update()</code> at the end of each of those three event handlers with and without calling base <code>QGraphicsItem::...Event(e)</code>. Didn't work</p>
]]></description><link>https://forum.qt.io/post/689798</link><guid isPermaLink="true">https://forum.qt.io/post/689798</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 12 Nov 2021 13:28:13 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 13:41:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a>, didn't have that in constructor. Tried this:</p>
<pre><code>Slice::Slice(float start, float sweep, QColor color, QRectF rect, PieSeries&amp; series, QGraphicsItem *parent)
    : QGraphicsItem(parent), m_start(start), m_sweep(sweep), m_color(color), m_series(series)
{...}
</code></pre>
<p dir="auto">with and without those base event calls mentioned in reply to @JonB. Didn't work.</p>
<p dir="auto"><strong>EDIT</strong><br />
Here's the whole content:</p>
<pre><code>#define whats16 16
Slice::Slice(float start, float sweep, QColor color, QRectF rect, PieSeries&amp; series, QGraphicsItem *parent)
    : QGraphicsItem(parent), m_start(start), m_sweep(sweep), m_color(color), m_series(series){
    m_realColor = color;
    m_rect = rect;
    m_path = QPainterPath(QPointF(m_rect.width() / 2, m_rect.height() / 2));
    m_path.arcTo(m_rect, -m_start, -m_sweep);
    m_path.closeSubpath();
    dx = 10 * cos((m_start + m_sweep / 2) * M_PI / 180);
    dy = 10 * sin((m_start + m_sweep / 2) * M_PI / 180);
    float dimension = m_rect.width() + 2 * 20;
    m_boundingRect = QRectF(-20, -20, dimension, dimension);
    setAcceptHoverEvents(true);
    setFlag(QGraphicsItem::ItemIsSelectable);
}
</code></pre>
<p dir="auto">EDIT</p>
<p dir="auto">and for the working example, you can get all code of different classes/struct <a href="https://forum.qt.io/topic/131930/how-do-you-center-qgraphicstextitem-in-its-parent/2">here</a></p>
]]></description><link>https://forum.qt.io/post/689797</link><guid isPermaLink="true">https://forum.qt.io/post/689797</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 12 Nov 2021 13:41:13 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 13:23:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/emon-haque">@<bdi>Emon-Haque</bdi></a></p>
<blockquote>
<p dir="auto">The default implementation calls update(); otherwise it does nothing.</p>
</blockquote>
<ul>
<li><a href="https://doc.qt.io/qt-5/qgraphicsitem.html#hoverEnterEvent" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qgraphicsitem.html#hoverEnterEvent</a></li>
</ul>
<p dir="auto">The default implementation will update your item. When you dont call it, it could lead to unwanted bahavior.</p>
]]></description><link>https://forum.qt.io/post/689796</link><guid isPermaLink="true">https://forum.qt.io/post/689796</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 12 Nov 2021 13:23:56 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 13:19:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a>, First, added <code>QGraphicsItem::...Event(e);</code> in the beginning of each function. Second, removed those from beginning and added at the end of each function and third, in addition to those in the end of <code>hoverEvents</code>, added same before <code>return</code>. Didn't work!</p>
]]></description><link>https://forum.qt.io/post/689795</link><guid isPermaLink="true">https://forum.qt.io/post/689795</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 12 Nov 2021 13:19:09 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 13:20:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/emon-haque">@<bdi>Emon-Haque</bdi></a></p>
<p dir="auto">Do you call the base class' c'tor in <code>Slice()</code> while creating your <code>Slice</code>?<br />
Also, like <a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said: Pass the event to the base class in your overriden functions (call the base event handler).<br />
E.g:</p>
<pre><code>void Slice::hoverEnterEvent(QGraphicsSceneHoverEvent* ev){
// ....
QGraphicsItem::hoverEnterEvent(ev);
}
</code></pre>
<p dir="auto">Edit:</p>
<p dir="auto">Show your <code>Slice</code> constructor</p>
]]></description><link>https://forum.qt.io/post/689794</link><guid isPermaLink="true">https://forum.qt.io/post/689794</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Fri, 12 Nov 2021 13:20:22 GMT</pubDate></item><item><title><![CDATA[Reply to How do you handle selection of QGraphicsItem? on Fri, 12 Nov 2021 12:59:56 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/emon-haque">@<bdi>Emon-Haque</bdi></a><br />
I have not followed your code.  But in each of these <code>...Event</code> overrides you do not allow the event to proceed through to default handling (by calling base method), is that what is causing whatever your behaviour is?</p>
]]></description><link>https://forum.qt.io/post/689789</link><guid isPermaLink="true">https://forum.qt.io/post/689789</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Fri, 12 Nov 2021 12:59:56 GMT</pubDate></item></channel></rss>