<?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[Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem]]></title><description><![CDATA[<p dir="auto">I want to set the QGraphicsScene‘s background image，But this will affect the item</p>
<p dir="auto">The code is similar:</p>
<pre><code>// Scene
void CGraphicsScene::drawBackground(QPainter *painter, const QRectF &amp;rect)
{
    painter-&gt;drawImage(rect, QImage(":/background/background.jpg"));
}

bool CGraphicsScene::event(QEvent *event)
{
    switch (event-&gt;type()){
    case QEvent::GraphicsSceneMousePress : {
        QGraphicsSceneMouseEvent * pMouseEvent = dynamic_cast&lt;QGraphicsSceneMouseEvent*&gt;(event);
        CGraphicsItem *pItem = new CGraphicsItem(pMouseEvent-&gt;scenePos().x(), pMouseEvent-&gt;scenePos().y());
        this-&gt;addItem(pItem);
        pItem-&gt;setPos(pMouseEvent-&gt;scenePos());
        m_vtItems.push_back(pItem);
        break;
    }
    default:
        break;
    }
    return QGraphicsScene::event(event);
}

// item
QRectF CGraphicsItem::boundingRect() const
{
    qreal penWidth = 1;
    return QRectF((-m_siWidth - penWidth) / 2, (-m_siHeight - penWidth) / 2, m_siWidth + penWidth,
                  m_siHeight + penWidth);
}

void CGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter-&gt;setRenderHint(QPainter::Antialiasing, true); 
    QPainterPath oEllipsePath;
    QRectF oEllipseRect = { -m_siWidth/2, -m_siHeight/2, m_siWidth, m_siHeight };
    oEllipsePath.addEllipse(oEllipseRect);
    painter-&gt;fillPath(oEllipsePath, Qt::black);
}
</code></pre>
<p dir="auto">Running diagram：<br />
![alt text](<img src="https://ddgobkiprc33d.cloudfront.net/06de2395-147c-4fa8-bb41-9b6a61d2d741.png" alt="image.png" class=" img-fluid img-markdown" /> image url)</p>
<p dir="auto">Whenever I add an item, QGraphicsScene::drawBackground will be triggered to draw the item area.</p>
]]></description><link>https://forum.qt.io/topic/126908/rewriting-qgraphicsscene-drawbackground-causes-background-problems-in-custom-drawing-qgraphicsitem</link><generator>RSS for Node</generator><lastBuildDate>Wed, 23 Sep 2026 17:01:52 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/126908.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 May 2021 08:34:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem on Sun, 23 May 2021 17:57:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> OK, I will try what you say, Thanks</p>
]]></description><link>https://forum.qt.io/post/661164</link><guid isPermaLink="true">https://forum.qt.io/post/661164</guid><dc:creator><![CDATA[MaMingKun]]></dc:creator><pubDate>Sun, 23 May 2021 17:57:22 GMT</pubDate></item><item><title><![CDATA[Reply to Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem on Sun, 23 May 2021 16:14:06 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mamingkun">@<bdi>MaMingKun</bdi></a></p>
<p dir="auto">Hi<br />
I think a  background image is fine. that function for such things.<br />
You just need to fix the code so it only draws what is needed when the rest<br />
is much smaller than the area.<br />
Your current<br />
painter-&gt;drawImage(rect,... )<br />
will scale the image to the rect.<br />
So you need to keep as a member variable<br />
and copy the rect needed to draw in drawBackground</p>
]]></description><link>https://forum.qt.io/post/661151</link><guid isPermaLink="true">https://forum.qt.io/post/661151</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 23 May 2021 16:14:06 GMT</pubDate></item><item><title><![CDATA[Reply to Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem on Sun, 23 May 2021 11:02:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mrjj">@<bdi>mrjj</bdi></a> Thank you, Is it better to use QGraphicsItem as the background?</p>
]]></description><link>https://forum.qt.io/post/661124</link><guid isPermaLink="true">https://forum.qt.io/post/661124</guid><dc:creator><![CDATA[MaMingKun]]></dc:creator><pubDate>Sun, 23 May 2021 11:02:24 GMT</pubDate></item><item><title><![CDATA[Reply to Rewriting QGraphicsScene::drawBackground causes background problems in custom drawing QGraphicsItem on Sun, 23 May 2021 09:02:06 GMT]]></title><description><![CDATA[<p dir="auto">hi<br />
Well that is expected.<br />
IF you look at<br />
<a href="https://doc.qt.io/qt-5/qgraphicsscene.html#drawBackground" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qgraphicsscene.html#drawBackground</a></p>
<p dir="auto">it says "The rect parameter is the exposed rectangle."  that is the parameter rect tells the area that needs repainting. ( like a dirty rect)</p>
<p dir="auto">So when you add an item its bounding box needs to be repainted.</p>
<p dir="auto">So you can use the<br />
<a href="https://doc.qt.io/qt-5/qimage.html#copy" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qimage.html#copy</a></p>
<p dir="auto">to get the part of the image that needs to be updated/redrawn.</p>
<p dir="auto">Currently, you will draw the entire image into the rect. which works good first time but when<br />
rect becomes a dirty rect for new item. it's not what you want.</p>
]]></description><link>https://forum.qt.io/post/661111</link><guid isPermaLink="true">https://forum.qt.io/post/661111</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Sun, 23 May 2021 09:02:06 GMT</pubDate></item></channel></rss>