<?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[Error Msg: QGraphicsScene::addItem: item has already been added to this scene]]></title><description><![CDATA[<p dir="auto">Hello everyone,</p>
<p dir="auto">I have a class that is derived from <code>QGraphicsView</code>. In this class I create a <code>QGraphicsLineItem</code>. If I create an instance of  <code>QGraphicsLineItem</code> in the constructor of the class and add items, I get the message <code>"QGraphicsScene::addItem: item has already been added to this scene"</code>. Also, no item is added to the scene.</p>
<p dir="auto">If I create the instance of the graphics item locally the items are added to the scene.<br />
I found <a href="https://stackoverflow.com/questions/33452642/qgraphicssceneadditem-item-has-already-been-added-to-this-scene" target="_blank" rel="noopener noreferrer nofollow ugc">this posts</a> but I did not understand the explantion. It sounds like that an instance of the graphics item should not created in the constructor.</p>
<p dir="auto">In fact I need the <code>QGraphicsLineItem</code> as a class member, because I want to use the instance of it in more than one function.</p>
<pre><code>//this did not work, I get the message
View::View(QWidget* parent) :
	QGraphicsView(parent), 
        m_lineItem(new QGraphicsLineItem)
{}

//this works
void View::createLineItem(QString name, QString nameStruct, QColor lineColor, int line)
{
	if (name == nameStruct) {
		m_lineItem = new QGraphicsLineItem;
		m_lineItem-&gt;setPos(0, 0);
		m_lineItem-&gt;setCursor(Qt::CrossCursor);
		m_lineItem-&gt;setPen(lineColor);
		m_lineItem-&gt;setLine(0, line, 130, line);
		m_lineItem-&gt;setToolTip(nameStruct);
		m_scene-&gt;addItem(m_lineItem);
	}
}
</code></pre>
<p dir="auto">Kind regards</p>
]]></description><link>https://forum.qt.io/topic/139260/error-msg-qgraphicsscene-additem-item-has-already-been-added-to-this-scene</link><generator>RSS for Node</generator><lastBuildDate>Sat, 31 Oct 2026 03:16:01 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139260.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 12 Sep 2022 17:24:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Thu, 15 Sep 2022 12:19:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/makopo">@<bdi>makopo</bdi></a> said in <a href="/post/728597">Error Msg: QGraphicsScene::addItem: item has already been added to this scene</a>:</p>
<blockquote>
<p dir="auto">Found that this as parameter of QGraphicsLineItem in LineItem.cpp produces the message.</p>
</blockquote>
<p dir="auto">Yes and this is wrong like <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> pointed out...</p>
]]></description><link>https://forum.qt.io/post/728848</link><guid isPermaLink="true">https://forum.qt.io/post/728848</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Thu, 15 Sep 2022 12:19:12 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Tue, 13 Sep 2022 17:02:07 GMT]]></title><description><![CDATA[<p dir="auto">How is your LineItem constructor declared in its header file? For graphics items, the last parameter is typically <code>QGraphicsItem *parent = nullptr</code> and then in the constructor definition you would have <code>:QGraphicsLineItem(parent)</code>.</p>
]]></description><link>https://forum.qt.io/post/728602</link><guid isPermaLink="true">https://forum.qt.io/post/728602</guid><dc:creator><![CDATA[mchinand]]></dc:creator><pubDate>Tue, 13 Sep 2022 17:02:07 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Tue, 13 Sep 2022 16:41:33 GMT]]></title><description><![CDATA[<p dir="auto">Found that <code>this</code> as parameter of <code>QGraphicsLineItem</code> in LineItem.cpp produces the message.</p>
]]></description><link>https://forum.qt.io/post/728597</link><guid isPermaLink="true">https://forum.qt.io/post/728597</guid><dc:creator><![CDATA[makopo]]></dc:creator><pubDate>Tue, 13 Sep 2022 16:41:33 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Tue, 13 Sep 2022 16:41:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/makopo">@<bdi>makopo</bdi></a> said in <a href="/post/728594">Error Msg: QGraphicsScene::addItem: item has already been added to this scene</a>:</p>
<blockquote>
<p dir="auto">I got another message: "QGraphicsItem::setParentItem: cannot assign 0x2690126ec50 as a parent of itself"</p>
</blockquote>
<blockquote>
<p dir="auto">LineItem::LineItem(QString name, QString packetName, const QColor&amp; color, int line) :<br />
QGraphicsLineItem(this),</p>
</blockquote>
<p dir="auto">And the warning is correct - why do you set the parent to yourself?</p>
]]></description><link>https://forum.qt.io/post/728596</link><guid isPermaLink="true">https://forum.qt.io/post/728596</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Tue, 13 Sep 2022 16:41:14 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Tue, 13 Sep 2022 16:36:13 GMT]]></title><description><![CDATA[<p dir="auto">If I create a class <code>LineItem</code> that is derived from <code>QGraphicsLineItem</code> and create an instance of it in the View class I got another message: <code>"QGraphicsItem::setParentItem: cannot assign 0x2690126ec50 as a parent of itself"</code></p>
<p dir="auto">I know that LineItem is a parent, bit what is the difference between the following code:</p>
<pre><code>//LineItem is derived from QGraphicsLineItem
void View::createLineItems(QString name, QString nameStruct, QColor lineColor, int line)
{
	if (name == nameStruct) {
		LineItem *lineItem = new LineItem(name, nameStruct, lineColor, line);
		m_scene-&gt;addItem(lineItem);
	}
}

void View::createLineItems(QString name, QString nameStruct, QColor lineColor, int line)
{
	if (name == nameStruct) {
		m_lineItem = new QGraphicsLineItem;
		m_lineItem-&gt;setPos(0, 0);
		m_lineItem-&gt;setCursor(Qt::CrossCursor);
		m_lineItem-&gt;setPen(lineColor);
		m_lineItem-&gt;setLine(0, line, 130, line);
		m_lineItem-&gt;setToolTip(nameStruct);
		m_scene-&gt;addItem(m_lineItem);
	}
}
</code></pre>
<p dir="auto">My LineItem class is defined as follows:</p>
<pre><code>//LineItem.cpp
LineItem::LineItem(QString name, QString packetName, const QColor&amp; color, int line) :
	QGraphicsLineItem(this),
	m_name(name),
	m_packetName(packetName),
	m_color(color),
	m_line(line)
{
	QGraphicsLineItem::setPos(0, 0);
	QGraphicsLineItem::setToolTip(packetName);
}

LineItem::~LineItem()
{
}

QRectF LineItem::boundingRect() const
{
	return QRectF();
}

void LineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *item, QWidget *widget)
{
	if (m_name == m_packetName) {
		painter-&gt;setPen(m_color);
		painter-&gt;drawLine(0, m_line, 130, m_line);
	}
}
</code></pre>
]]></description><link>https://forum.qt.io/post/728594</link><guid isPermaLink="true">https://forum.qt.io/post/728594</guid><dc:creator><![CDATA[makopo]]></dc:creator><pubDate>Tue, 13 Sep 2022 16:36:13 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Tue, 13 Sep 2022 08:25:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">I have  a combo box where the user can select different categories. For example categorie A has 5 lines and categorie Z has 1000 lines. So if the user switches from Z to A all lines have to be removed and new lines have to be added. Sounds that <code>QGraphicItem</code> is the correct interface for this job.</p>
]]></description><link>https://forum.qt.io/post/728494</link><guid isPermaLink="true">https://forum.qt.io/post/728494</guid><dc:creator><![CDATA[makopo]]></dc:creator><pubDate>Tue, 13 Sep 2022 08:25:18 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Tue, 13 Sep 2022 08:16:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/makopo">@<bdi>makopo</bdi></a><br />
It is OK, in the sense that <a href="https://doc.qt.io/qt-6/qgraphicsscene.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">QGraphicsScene</a> states e.g.</p>
<blockquote>
<p dir="auto">One of QGraphicsScene's greatest strengths is its ability to efficiently determine the location of items. Even with millions of items on the scene, the items() functions can determine the location of an item within a few milliseconds.</p>
</blockquote>
<p dir="auto">Having <code>QGraphicsItem</code>s for, say, lines is useful if you want to manipulate them, e.g. add/remove/move them, test where they are, or add other shape items.  If you only want them to be visible as a "static background", without any further processing, then you might choose instead to <em>paint</em> them, either on a graphics background or foreground or into a widget/paint device.  <em>Painted</em> items cannot be retrieved/manipulated.  Depends on your usage.</p>
]]></description><link>https://forum.qt.io/post/728492</link><guid isPermaLink="true">https://forum.qt.io/post/728492</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 13 Sep 2022 08:16:41 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Tue, 13 Sep 2022 07:54:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a><br />
Creating nearly 1000 instances of <code>QGraphicsLineItem</code> is a good idea?</p>
]]></description><link>https://forum.qt.io/post/728489</link><guid isPermaLink="true">https://forum.qt.io/post/728489</guid><dc:creator><![CDATA[makopo]]></dc:creator><pubDate>Tue, 13 Sep 2022 07:54:21 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Mon, 12 Sep 2022 17:57:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/makopo">@<bdi>makopo</bdi></a> said in <a href="/post/728428">Error Msg: QGraphicsScene::addItem: item has already been added to this scene</a>:</p>
<blockquote>
<p dir="auto">So I think the drawLines(...) method of QPainter can help me?</p>
</blockquote>
<p dir="auto">I don't understand - when you want to show more than one line in your scene then create more QGraphicsLineItem instances.</p>
]]></description><link>https://forum.qt.io/post/728429</link><guid isPermaLink="true">https://forum.qt.io/post/728429</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Mon, 12 Sep 2022 17:57:03 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Mon, 12 Sep 2022 17:51:36 GMT]]></title><description><![CDATA[<p dir="auto">So I think the <code>drawLines(...)</code> method of <code>QPainter</code> can help me?</p>
]]></description><link>https://forum.qt.io/post/728428</link><guid isPermaLink="true">https://forum.qt.io/post/728428</guid><dc:creator><![CDATA[makopo]]></dc:creator><pubDate>Mon, 12 Sep 2022 17:51:36 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Mon, 12 Sep 2022 17:47:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a></p>
<p dir="auto">Thanks for your input.</p>
<p dir="auto">I created a loop and inside of it the y values of a line are set new by every interation. So I get a big number of lines displayed. Never thought about that is is not the correct way.</p>
]]></description><link>https://forum.qt.io/post/728426</link><guid isPermaLink="true">https://forum.qt.io/post/728426</guid><dc:creator><![CDATA[makopo]]></dc:creator><pubDate>Mon, 12 Sep 2022 17:47:38 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Mon, 12 Sep 2022 17:38:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/makopo">@<bdi>makopo</bdi></a> said in <a href="/post/728424">Error Msg: QGraphicsScene::addItem: item has already been added to this scene</a>:</p>
<blockquote>
<p dir="auto">Its not possible to at more than one line item to the view with one instance?</p>
</blockquote>
<p dir="auto">´No, how should this be possible - how would they be distinguishable? And it's exact what the error tells you.</p>
]]></description><link>https://forum.qt.io/post/728425</link><guid isPermaLink="true">https://forum.qt.io/post/728425</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Mon, 12 Sep 2022 17:38:12 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Mon, 12 Sep 2022 17:37:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a></p>
<p dir="auto">With "create an instance in the constructor" I mean that I create an instance in the Initializer list of the constructor</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said in <a href="/post/728423">Error Msg: QGraphicsScene::addItem: item has already been added to this scene</a>:</p>
<blockquote>
<p dir="auto">You can not add a graphicsitem twice to a view - you have to create different instances if you want to add more than one item.</p>
</blockquote>
<p dir="auto">Its not possible to at more than one line item to the view with one instance? I have a wide range of lines I want to show in  a graphics scene (min. 5 - max. 1000).  If I create the instance of <code>QGraphicsLineItem</code> in the function it is possible to add 1000 lines to the graphics scene.</p>
<p dir="auto">If this is not the correct workflow: what is the correct way?</p>
]]></description><link>https://forum.qt.io/post/728424</link><guid isPermaLink="true">https://forum.qt.io/post/728424</guid><dc:creator><![CDATA[makopo]]></dc:creator><pubDate>Mon, 12 Sep 2022 17:37:10 GMT</pubDate></item><item><title><![CDATA[Reply to Error Msg: QGraphicsScene::addItem: item has already been added to this scene on Mon, 12 Sep 2022 17:29:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/makopo">@<bdi>makopo</bdi></a> said in <a href="/post/728421">Error Msg: QGraphicsScene::addItem: item has already been added to this scene</a>:</p>
<blockquote>
<p dir="auto">If I create an instance of  QGraphicsLineItem in the constructor of the class and add items</p>
</blockquote>
<p dir="auto">What does this mean? Your code doesn't show something similar like you explain here.<br />
You can not add a graphicsitem twice to a view - you have to create different instances if you want to add more than one item.</p>
]]></description><link>https://forum.qt.io/post/728423</link><guid isPermaLink="true">https://forum.qt.io/post/728423</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Mon, 12 Sep 2022 17:29:02 GMT</pubDate></item></channel></rss>