Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qgraphicsscene remove item but sometime still in view
QtWS25 Last Chance

qgraphicsscene remove item but sometime still in view

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 2.1k Views
  • 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.
  • C Offline
    C Offline
    choujayylyxm
    wrote on last edited by choujayylyxm
    #1

    I use a qgraphicsscene additem a QChart and some inhert from the QChart item.I change QChart axisX and axisY range,and removeitem those items,then add new item.But sometime removeitem not working,item still in view.I cannot move it now.
    And also call all of items prepareGeometryChange,not work.
    So,why?

    I test long time,found the point is set range.if resize the view,the removed item disappear.

    Qt 5.9.5 kits

    1 Reply Last reply
    0
    • C Offline
      C Offline
      choujayylyxm
      wrote on last edited by
      #2

      someone metion :https://www.qtcentre.org/threads/28749-Qt-4-6-GraphicsView-items-remove-problem

      1 Reply Last reply
      0
      • C Offline
        C Offline
        choujayylyxm
        wrote on last edited by
        #3
        This post is deleted!
        SGaistS 1 Reply Last reply
        0
        • C choujayylyxm

          This post is deleted!

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          @choujayylyxm said in qgraphicsscene remove item but sometime still in view:

          No one reply?!

          No need to resort to bold fonts and such questions. A friendly ping is welcome though. This is a voluntary driven forum. People answering here do it on their own time and might not live in the same timezone as you. And it's also possible that no one has an answer for you.

          In any case, you should provide a minimal compilable example that reproduces your issue.
          You should also test your code against a more recent version of Qt, 5.9.5 is more than outdated. Qt 5.15 is the current LTS. Providing the platform your code runs on would also be good idea.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • C Offline
            C Offline
            choujayylyxm
            wrote on last edited by choujayylyxm
            #5

            Ok,I understood.Sorry for that.
            Here is a simple example:

               //click a button to switch line and run below code
              //  a QGraphicsView scene()->addItem(m_chart);
              //replace a new line
              QLineSeries * tmp = static_cast<QLineSeries *>(m_chart->series()[0]);
              QList<QPointF> dataPointList;
              //set new axis range
              m_chart->axisX()->setRange(0,dataVec[dataVec.size()-1].x());
              m_chart->axisY()->setRange(-qAbs(yMax)*1.2f, qAbs(yMax)*1.2f);
              tmp->replace(dataPointList);
              
              
              //delete last line a flag(circle)item in the qchart ,m_calloutsList is keep flags list
              for(auto& item : m_calloutsList[nDataIndex])
              {
                  //item.prepareGeomtryChange //try it not work
                  scene()->removeItem(item);
              }
              //add current line flag item
              for(auto& item2 : m_calloutsList[nDataIndex])
              {
              //item2.prepareGeomtryChange //try it not work
                  scene()->addItem(item2);
              }
            

            1.png

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Please provide a minimal compilable example that shows the behavior.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply
              0
              • C Offline
                C Offline
                choujayylyxm
                wrote on last edited by choujayylyxm
                #7

                @SGaist OK,I upload a minimal compilable example for github
                github link

                Is it okay? It base on qt example callout

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Please provide a minimal compilable example that shows the behavior.

                  C Offline
                  C Offline
                  choujayylyxm
                  wrote on last edited by
                  #8

                  @SGaist Fine,I keep working on it.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Did you try to call hide on your items before removing them from the scene ?

                    One thing: there's no need to make the QGraphicsItemObject. A simple QGraphicsRectItem is enough for your purpose.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    S 1 Reply Last reply
                    0
                    • Ketan__Patel__0011K Offline
                      Ketan__Patel__0011K Offline
                      Ketan__Patel__0011
                      wrote on last edited by
                      #10

                      try to After

                      remove the item from your qgraphicsscene

                              scene()->removeItem(YOURITEM_OBJECT);
                              ui->graphicsview->viewport()->update();
                              ui->graphicsview->update();
                              ui->graphicsview->show();
                      
                      1 Reply Last reply
                      0
                      • SGaistS SGaist

                        Did you try to call hide on your items before removing them from the scene ?

                        One thing: there's no need to make the QGraphicsItemObject. A simple QGraphicsRectItem is enough for your purpose.

                        S Offline
                        S Offline
                        StudentScripter
                        wrote on last edited by StudentScripter
                        #11

                        @SGaist Having a question regarding calling hide cause i encounter similar problems.
                        In my scene i want to add some items when another item is clicked, but when i unselect the other items the items added shall be removed aswell.

                        NOW as i figured when just calling remove onto the other items the programm crashes cause there is still the flag "isVisible" given back on remove.

                        Is it in this case fine to just case setVisible(false) and than scene()->removeItem(), because than it don't crashes. But im not sure if thats proper way to do it or is it just a whacky fix.

                        Here is my code that draws the items when the Owner item is clicked:

                        void ITMHandleDraw::drawHandlesIfNecessary()
                        {
                            //qDebug() << "DrawHandles" << ShouldDrawHandles << ownerItem->isSelected();
                            if(!ownerItem){
                                qWarning() << "ITMHandleDraw : No ownerItem set. Not drawing any\
                                              handle. Please call setOwnerItem on your ITMHandleDraw subclass";
                                              return;
                            }
                        
                            if(ownerItem->isSelected()){
                                drawHandles();
                        
                            }else if(!ownerItem->isSelected()){
                                handlesAddedToScene = false;
                        
                                // Ensure handles are removed from the scene before deletion
                                //Remove the handles
                                foreach (ITMHandleLogic * ITMHandleLogic, handleList) {
                                   // ITMHandleLogic->setVisible(false);
                        
                                    ITMHandleLogic->scene()->removeItem(ITMHandleLogic);
                                    qDebug() << ITMHandleLogic->isVisible();
                                }
                                    qDeleteAll(handleList);
                                    handleList.clear();
                        
                            }
                        
                        }
                        
                        
                        jsulmJ JonBJ 2 Replies Last reply
                        0
                        • S StudentScripter

                          @SGaist Having a question regarding calling hide cause i encounter similar problems.
                          In my scene i want to add some items when another item is clicked, but when i unselect the other items the items added shall be removed aswell.

                          NOW as i figured when just calling remove onto the other items the programm crashes cause there is still the flag "isVisible" given back on remove.

                          Is it in this case fine to just case setVisible(false) and than scene()->removeItem(), because than it don't crashes. But im not sure if thats proper way to do it or is it just a whacky fix.

                          Here is my code that draws the items when the Owner item is clicked:

                          void ITMHandleDraw::drawHandlesIfNecessary()
                          {
                              //qDebug() << "DrawHandles" << ShouldDrawHandles << ownerItem->isSelected();
                              if(!ownerItem){
                                  qWarning() << "ITMHandleDraw : No ownerItem set. Not drawing any\
                                                handle. Please call setOwnerItem on your ITMHandleDraw subclass";
                                                return;
                              }
                          
                              if(ownerItem->isSelected()){
                                  drawHandles();
                          
                              }else if(!ownerItem->isSelected()){
                                  handlesAddedToScene = false;
                          
                                  // Ensure handles are removed from the scene before deletion
                                  //Remove the handles
                                  foreach (ITMHandleLogic * ITMHandleLogic, handleList) {
                                     // ITMHandleLogic->setVisible(false);
                          
                                      ITMHandleLogic->scene()->removeItem(ITMHandleLogic);
                                      qDebug() << ITMHandleLogic->isVisible();
                                  }
                                      qDeleteAll(handleList);
                                      handleList.clear();
                          
                              }
                          
                          }
                          
                          
                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @StudentScripter said in qgraphicsscene remove item but sometime still in view:

                          foreach (ITMHandleLogic * ITMHandleLogic, handleList) {

                          Is this your real code?! Because it can't even compile.
                          I also don't get what this line is supposed to do:

                          ITMHandleLogic->scene()->removeItem(ITMHandleLogic);
                          

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

                          JonBJ 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @StudentScripter said in qgraphicsscene remove item but sometime still in view:

                            foreach (ITMHandleLogic * ITMHandleLogic, handleList) {

                            Is this your real code?! Because it can't even compile.
                            I also don't get what this line is supposed to do:

                            ITMHandleLogic->scene()->removeItem(ITMHandleLogic);
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @jsulm said in qgraphicsscene remove item but sometime still in view:

                            foreach (ITMHandleLogic * ITMHandleLogic, handleList) {

                            Is this your real code?! Because it can't even compile.

                            Don't know if you maybe thought this was a for, but it's a foreach and compiles fine? As does e.g.

                            QList<QString *> QStringList;
                            foreach (QString * QString, QStringList) {
                            

                            Unfortunately the OP has a predilection for naming variables identically to their types, which is not helpful but (at least in this case) works fine.

                            I also don't get what this line is supposed to do:

                            ITMHandleLogic->scene()->removeItem(ITMHandleLogic);

                            Given ITMHandleLogic which is a pointer to a QGraphicsItem this gets to the QGraphicsScene from it via QGraphicsItem::scene() to call QGraphicsItem::removeItem(ITMHandleLogic). Alternatively the OP could just have called delete ITMHandleLogic to delete and remove (per Qt internal code for QGraphicsItem destructor which removes from scene).

                            1 Reply Last reply
                            1
                            • S StudentScripter

                              @SGaist Having a question regarding calling hide cause i encounter similar problems.
                              In my scene i want to add some items when another item is clicked, but when i unselect the other items the items added shall be removed aswell.

                              NOW as i figured when just calling remove onto the other items the programm crashes cause there is still the flag "isVisible" given back on remove.

                              Is it in this case fine to just case setVisible(false) and than scene()->removeItem(), because than it don't crashes. But im not sure if thats proper way to do it or is it just a whacky fix.

                              Here is my code that draws the items when the Owner item is clicked:

                              void ITMHandleDraw::drawHandlesIfNecessary()
                              {
                                  //qDebug() << "DrawHandles" << ShouldDrawHandles << ownerItem->isSelected();
                                  if(!ownerItem){
                                      qWarning() << "ITMHandleDraw : No ownerItem set. Not drawing any\
                                                    handle. Please call setOwnerItem on your ITMHandleDraw subclass";
                                                    return;
                                  }
                              
                                  if(ownerItem->isSelected()){
                                      drawHandles();
                              
                                  }else if(!ownerItem->isSelected()){
                                      handlesAddedToScene = false;
                              
                                      // Ensure handles are removed from the scene before deletion
                                      //Remove the handles
                                      foreach (ITMHandleLogic * ITMHandleLogic, handleList) {
                                         // ITMHandleLogic->setVisible(false);
                              
                                          ITMHandleLogic->scene()->removeItem(ITMHandleLogic);
                                          qDebug() << ITMHandleLogic->isVisible();
                                      }
                                          qDeleteAll(handleList);
                                          handleList.clear();
                              
                                  }
                              
                              }
                              
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @StudentScripter said in qgraphicsscene remove item but sometime still in view:

                              calling remove onto the other items the programm crashes cause there is still the flag "isVisible" given back on remove.

                              Is it in this case fine to just case setVisible(false) and than scene()->removeItem(), because than it don't crashes.

                              First, why do you care about isVisible() or setVisible() at all in your situation? Once you removeItem() the QGraphicsItem will no longer be shown on the scene/view so it's not relevant.

                              But given that you want to use it. After removeItem() the item no longer belongs to the scene in any way. I would guess that QGraphicsItem::visible() needs to access the scene from the item in order to do whatever calculations are required for "visibility". But for a removed item the scene is probably "undefined" (or maybe nullptr) and hence it "crashes". Do not call any methods on a QGraphicsItem once it has been removed from the scene. If you really want to set/test visibility do so before removeItem().

                              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 Extensions
                              • Unsolved