[Qt-interest] BUG? in QGraphicsView::setParentItem

Michael Berg Michael.Berg at web.de
Tue Feb 9 12:11:42 CET 2010


Hello,

after switching my application to QT 4.6.1 a problem appeared which I could trace back to setParentItem not correctly updating the items after switching parents.

below is a small example showing the effect:
don't forget to run
> moc main.cpp -o main.moc 
before


main.cpp -------------------------------------------------------

#include <QtGui/QtGui>

class X : public QObject
{
  Q_OBJECT
  QGraphicsScene& scene;
  QGraphicsRectItem *rect1, *rect2, *rect3;
  bool alternate;
public:
  X(QGraphicsScene& s) : scene(s), alternate(true)
  {
    rect1 = scene.addRect(0, 0, 50, 50);
    rect1->setPos(100, 0);
    rect2 = scene.addRect(0, 0 , 50, 50);
    rect2->setPos(0,100);
    rect3 = new QGraphicsRectItem(0, 0, 25, 25, rect1);
    rect3->setBrush(Qt::red);
  }
public slots:
  void exec()
  {
    if(alternate)
    {
      rect3->setParentItem(rect2);
      //rect1->update(rect1->boundingRect());
      //rect2->update(rect2->boundingRect());
    }
    else
    {
      rect3->setParentItem(rect1);
      //rect2->update(rect2->boundingRect());
      //rect1->update(rect1->boundingRect());
    }
    QTimer::singleShot(1000, this, SLOT(exec()));
    alternate = !alternate;
  }
};


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

  QGraphicsScene scene;
  QGraphicsView view(&scene);
  view.show();

  X x(scene);

  QTimer::singleShot(2000, &x, SLOT(exec()));

	app.exec();

	return 0;
}

#include "main.moc"



----------------------------------------------------------------------


It works if one removes the comments in if(alternate) statement and explicitly updates the new and old parent
or simply resize the view continuously by hand, thus having a scene update.
This used to work at least in 4.5.3 without problems.
I also checked the implementation, but there was too much going on to quickly find a reason
without studying it in depth.

Has anybody an idea, am I missing something, or is it simply a bug in QT 4.6.1 ?

Thanks for any help.



Michael



More information about the Qt-interest-old mailing list