Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error Msg: QGraphicsScene::addItem: item has already been added to this scene
Qt 6.12 LTS is out! See what's new in the release blog

Error Msg: QGraphicsScene::addItem: item has already been added to this scene

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 5 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    makopo
    wrote on last edited by
    #6

    So I think the drawLines(...) method of QPainter can help me?

    Christian EhrlicherC 1 Reply Last reply
    0
    • M makopo

      So I think the drawLines(...) method of QPainter can help me?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @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.

        M Offline
        M Offline
        makopo
        wrote on last edited by
        #8

        @Christian-Ehrlicher
        Creating nearly 1000 instances of QGraphicsLineItem is a good idea?

        JonBJ 1 Reply Last reply
        0
        • M makopo

          @Christian-Ehrlicher
          Creating nearly 1000 instances of QGraphicsLineItem is a good idea?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #9

          @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.

          M 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            M Offline
            M Offline
            makopo
            wrote on last edited by makopo
            #10

            @JonB

            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 QGraphicItem is the correct interface for this job.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              makopo
              wrote on last edited by
              #11

              If I create a class LineItem that is derived from QGraphicsLineItem and 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);
              	}
              }
              
              Christian EhrlicherC 1 Reply Last reply
              0
              • M makopo

                If I create a class LineItem that is derived from QGraphicsLineItem and 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);
                	}
                }
                
                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @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?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  makopo
                  wrote on last edited by
                  #13

                  Found that this as parameter of QGraphicsLineItem in LineItem.cpp produces the message.

                  M jsulmJ 2 Replies Last reply
                  0
                  • M makopo

                    Found that this as parameter of QGraphicsLineItem in LineItem.cpp produces the message.

                    M Offline
                    M Offline
                    mchinand
                    wrote on last edited by
                    #14

                    How is your LineItem constructor declared in its header file? For graphics items, the last parameter is typically QGraphicsItem *parent = nullptr and then in the constructor definition you would have :QGraphicsLineItem(parent).

                    1 Reply Last reply
                    3
                    • M makopo

                      Found that this as parameter of QGraphicsLineItem in LineItem.cpp produces the message.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #15

                      @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...

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt
                      • Unsolved