Error Msg: QGraphicsScene::addItem: item has already been added to this scene
-
Hello everyone,
I have a class that is derived from
QGraphicsView. In this class I create aQGraphicsLineItem. If I create an instance ofQGraphicsLineItemin the constructor of the class and add items, I get the message"QGraphicsScene::addItem: item has already been added to this scene". Also, no item is added to the scene.If I create the instance of the graphics item locally the items are added to the scene.
I found this posts but I did not understand the explantion. It sounds like that an instance of the graphics item should not created in the constructor.In fact I need the
QGraphicsLineItemas a class member, because I want to use the instance of it in more than one function.//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->setPos(0, 0); m_lineItem->setCursor(Qt::CrossCursor); m_lineItem->setPen(lineColor); m_lineItem->setLine(0, line, 130, line); m_lineItem->setToolTip(nameStruct); m_scene->addItem(m_lineItem); } }Kind regards
-
Hello everyone,
I have a class that is derived from
QGraphicsView. In this class I create aQGraphicsLineItem. If I create an instance ofQGraphicsLineItemin the constructor of the class and add items, I get the message"QGraphicsScene::addItem: item has already been added to this scene". Also, no item is added to the scene.If I create the instance of the graphics item locally the items are added to the scene.
I found this posts but I did not understand the explantion. It sounds like that an instance of the graphics item should not created in the constructor.In fact I need the
QGraphicsLineItemas a class member, because I want to use the instance of it in more than one function.//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->setPos(0, 0); m_lineItem->setCursor(Qt::CrossCursor); m_lineItem->setPen(lineColor); m_lineItem->setLine(0, line, 130, line); m_lineItem->setToolTip(nameStruct); m_scene->addItem(m_lineItem); } }Kind regards
@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
If I create an instance of QGraphicsLineItem in the constructor of the class and add items
What does this mean? Your code doesn't show something similar like you explain here.
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. -
@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
If I create an instance of QGraphicsLineItem in the constructor of the class and add items
What does this mean? Your code doesn't show something similar like you explain here.
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.With "create an instance in the constructor" I mean that I create an instance in the Initializer list of the constructor
@Christian-Ehrlicher said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
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.
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
QGraphicsLineItemin the function it is possible to add 1000 lines to the graphics scene.If this is not the correct workflow: what is the correct way?
-
With "create an instance in the constructor" I mean that I create an instance in the Initializer list of the constructor
@Christian-Ehrlicher said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
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.
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
QGraphicsLineItemin the function it is possible to add 1000 lines to the graphics scene.If this is not the correct workflow: what is the correct way?
@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
Its not possible to at more than one line item to the view with one instance?
´No, how should this be possible - how would they be distinguishable? And it's exact what the error tells you.
-
@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
Its not possible to at more than one line item to the view with one instance?
´No, how should this be possible - how would they be distinguishable? And it's exact what the error tells you.
Thanks for your input.
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.
-
@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
So I think the drawLines(...) method of QPainter can help me?
I don't understand - when you want to show more than one line in your scene then create more QGraphicsLineItem instances.
-
@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
So I think the drawLines(...) method of QPainter can help me?
I don't understand - when you want to show more than one line in your scene then create more QGraphicsLineItem instances.
@Christian-Ehrlicher
Creating nearly 1000 instances ofQGraphicsLineItemis a good idea? -
@Christian-Ehrlicher
Creating nearly 1000 instances ofQGraphicsLineItemis a good idea?@makopo
It is OK, in the sense that QGraphicsScene states e.g.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.
Having
QGraphicsItems 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 paint them, either on a graphics background or foreground or into a widget/paint device. Painted items cannot be retrieved/manipulated. Depends on your usage. -
@makopo
It is OK, in the sense that QGraphicsScene states e.g.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.
Having
QGraphicsItems 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 paint them, either on a graphics background or foreground or into a widget/paint device. Painted items cannot be retrieved/manipulated. Depends on your usage.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
QGraphicItemis the correct interface for this job. -
If I create a class
LineItemthat is derived fromQGraphicsLineItemand create an instance of it in the View class I got another message:"QGraphicsItem::setParentItem: cannot assign 0x2690126ec50 as a parent of itself"I know that LineItem is a parent, bit what is the difference between the following 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->addItem(lineItem); } } void View::createLineItems(QString name, QString nameStruct, QColor lineColor, int line) { if (name == nameStruct) { m_lineItem = new QGraphicsLineItem; m_lineItem->setPos(0, 0); m_lineItem->setCursor(Qt::CrossCursor); m_lineItem->setPen(lineColor); m_lineItem->setLine(0, line, 130, line); m_lineItem->setToolTip(nameStruct); m_scene->addItem(m_lineItem); } }My LineItem class is defined as follows:
//LineItem.cpp LineItem::LineItem(QString name, QString packetName, const QColor& 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->setPen(m_color); painter->drawLine(0, m_line, 130, m_line); } } -
If I create a class
LineItemthat is derived fromQGraphicsLineItemand create an instance of it in the View class I got another message:"QGraphicsItem::setParentItem: cannot assign 0x2690126ec50 as a parent of itself"I know that LineItem is a parent, bit what is the difference between the following 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->addItem(lineItem); } } void View::createLineItems(QString name, QString nameStruct, QColor lineColor, int line) { if (name == nameStruct) { m_lineItem = new QGraphicsLineItem; m_lineItem->setPos(0, 0); m_lineItem->setCursor(Qt::CrossCursor); m_lineItem->setPen(lineColor); m_lineItem->setLine(0, line, 130, line); m_lineItem->setToolTip(nameStruct); m_scene->addItem(m_lineItem); } }My LineItem class is defined as follows:
//LineItem.cpp LineItem::LineItem(QString name, QString packetName, const QColor& 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->setPen(m_color); painter->drawLine(0, m_line, 130, m_line); } }@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
I got another message: "QGraphicsItem::setParentItem: cannot assign 0x2690126ec50 as a parent of itself"
LineItem::LineItem(QString name, QString packetName, const QColor& color, int line) :
QGraphicsLineItem(this),And the warning is correct - why do you set the parent to yourself?
-
@makopo said in Error Msg: QGraphicsScene::addItem: item has already been added to this scene:
Found that this as parameter of QGraphicsLineItem in LineItem.cpp produces the message.
Yes and this is wrong like @Christian-Ehrlicher pointed out...