Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-6492

QGraphicsItem: setZvalue() to a previously unassigned number causes QGraphicsScene to crash when closing the application

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • Not Evaluated
    • 4.6.1
    • 4.6.0
    • Widgets: GraphicsView
    • None
    • Windows XP/Windows 7 VS2008
    • 705880f0045ac39140f980d69aec68869213e379

    Description

      If, for example, three QGraphicsItems are added to a scene and their z-value is set explicitly to 1, 2 and 3 respectively and I then move one of the existing QGraphicsItems to z-value 0, the scene crashes in its destructor when closing the application.
      To reproduce, use the following sample code:

      //#include <QtGui>
      //
      //static QGraphicsScene *myScene;
      //static QList<QGraphicsRectItem *> *myList;
      //
      //class GraphicsView: public QGraphicsView
      //{
      // Q_OBJECT
      //
      //public:
      // GraphicsView(QGraphicsScene *scene, QWidget *parent):
      // QGraphicsView(scene,parent) {}
      //
      //public slots:
      // void moveToBackground();
      //};
      //
      //void GraphicsView::moveToBackground()
      //

      { // QGraphicsItem *item=myList->at(2); // //myScene->removeItem(item); // item->setZValue(0); // //myScene->addItem(item); //}

      //
      //#include "main.moc"
      //
      //int main(int argc, char **argv)
      //{
      // QApplication app(argc, argv);
      //
      //
      // myScene= new QGraphicsScene;
      // myScene->setSceneRect(0, 0, 800, 480);
      // myScene->setItemIndexMethod(QGraphicsScene::NoIndex);
      //
      // QList<QGraphicsRectItem *> list;
      // myList=&list;
      // list.append(new QGraphicsRectItem(10,15,20,25));
      // list.append(new QGraphicsRectItem(20,17,20,25));
      // list.append(new QGraphicsRectItem(14,24,20,25));
      //
      // QColor colors[3]=

      { // QColor(Qt::red), //z==1 // QColor(Qt::green), //z==2 // QColor(Qt::blue) //z==3 // }

      ;
      //
      // for (int i=0; i < 3; ++i)

      { // list[i]->setFlag(QGraphicsItem::ItemIsMovable); // list[i]->setFlag(QGraphicsItem::ItemIsSelectable); // list[i]->setZValue(i+1); // list[i]->setPen(QPen(colors[i])); // myScene->addItem(list[i]); // }

      // myScene->setBackgroundBrush(Qt::white);
      // GraphicsView *view = new GraphicsView(myScene,NULL);
      // QAction *pAct=new QAction(view);
      // new QShortcut(QString::fromLatin1("A"),view,SLOT(moveToBackground()),SLOT(moveToBackground()));
      // QObject::connect(pAct,SIGNAL(triggered()),view,SLOT(moveToBackground()));
      //
      // view->show();
      // return app.exec();
      // myScene->clear();
      // return 0;
      //}

      #include <QtGui>

      static QGraphicsScene *myScene;
      static QList<QGraphicsRectItem *> *myList;

      class GraphicsView: public QGraphicsView
      {
      Q_OBJECT

      public:
      GraphicsView(QGraphicsScene *scene, QWidget *parent):
      QGraphicsView(scene,parent) {}

      public slots:
      void moveToBackground();
      void sceneDestroyed();
      };

      void GraphicsView::moveToBackground()
      {
      QGraphicsItem *item=myList->at(2);
      //myScene->removeItem(item);
      item->setZValue(0);
      //myScene->addItem(item);

      }
      void GraphicsView::sceneDestroyed()
      {
      qDebug() << "scene destroyed";
      }
      #include "main.moc"

      int main(int argc, char **argv)
      {
      QApplication app(argc, argv);

      QGraphicsScene scene;
      myScene=&scene;
      scene.setSceneRect(0, 0, 800, 480);
      scene.setItemIndexMethod(QGraphicsScene::NoIndex);

      QList<QGraphicsRectItem *> list;
      myList=&list;
      list.append(new QGraphicsRectItem(10,15,20,25));
      list.append(new QGraphicsRectItem(20,17,20,25));
      list.append(new QGraphicsRectItem(14,24,20,25));

      QColor colors[3]=

      { QColor(Qt::red), //z==1 QColor(Qt::green), //z==2 QColor(Qt::blue) //z==3 }

      ;

      for (int i=0; i < 3; ++i)

      { list[i]->setFlag(QGraphicsItem::ItemIsMovable); list[i]->setFlag(QGraphicsItem::ItemIsSelectable); list[i]->setZValue(i+1); list[i]->setPen(QPen(colors[i])); scene.addItem(list[i]); qDebug() << list[i]->zValue(); }

      scene.setBackgroundBrush(Qt::white);
      GraphicsView *view = new GraphicsView(&scene,NULL);
      QAction *pAct=new QAction(view);
      new QShortcut(QString::fromLatin1("A"),view,SLOT(moveToBackground()),SLOT(moveToBackground()));
      QObject::connect(pAct,SIGNAL(triggered()),view,SLOT(moveToBackground()));
      QObject::connect(qobject_cast<QObject*>(myScene),SIGNAL(destroyed()),view,SLOT(sceneDestroyed()));
      view->show();
      return app.exec();
      }

      Press "a" on the keyboard to trigger the slot which changes the z-order of the last item in the list. Then close the application. A crash is seen which originates in the destructor of QGraphicsScene.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ylopes Yoann Lopes
            cattell Matthew Cattell (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes