Error Msg: QGraphicsScene::addItem: item has already been added to this scene
-
@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...