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.1k 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
    #1

    Hello everyone,

    I have a class that is derived from QGraphicsView. In this class I create a QGraphicsLineItem. If I create an instance of QGraphicsLineItem in 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 QGraphicsLineItem as 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

    Christian EhrlicherC 1 Reply Last reply
    0
    • M makopo

      Hello everyone,

      I have a class that is derived from QGraphicsView. In this class I create a QGraphicsLineItem. If I create an instance of QGraphicsLineItem in 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 QGraphicsLineItem as 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

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

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

      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
      2
      • Christian EhrlicherC Christian Ehrlicher

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

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

        @Christian-Ehrlicher

        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 QGraphicsLineItem in 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?

        Christian EhrlicherC 1 Reply Last reply
        0
        • M makopo

          @Christian-Ehrlicher

          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 QGraphicsLineItem in 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?

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

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

          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:

            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.

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

            @Christian-Ehrlicher

            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.

            1 Reply Last reply
            0
            • 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