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

[Regression] When using the DontSavePainterState optimization flags, the painter pass in drawForeground is translated when it shouldn't

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: P1: Critical P1: Critical
    • 4.6.0
    • 4.6.0
    • Widgets: GraphicsView
    • None
    • 72fb0f2637db401efd178b9d4139fc2b6ef59112

      When using the DontSavePainterState optimization flags, the painter pass in drawForeground is translated when it shouldn't.

      One item is moved calling setPos and then the painter get translated in drawForeground. The code was working fine in 4.5. DontSavePainterState is only for transformation that the user applies not for ours...

      #include <QGraphicsView>
      #include <QGraphicsPixmapItem>
      #include <QApplication>
      
      /*
       * Bug: when view has DontSavePainterState optimization and scene has some item for which setPos() is
       * called at some point, then QPainter which is passed to drawForeground() will have translated
       * origin. AND origin will be translated only if view gets large enough size.
       *
       * Try to resize view widget in this example to smaller size and you'll notice that the green
       * square which is drawn in drawForeground() will jump to 0,0 at which it should be.
       *
       * If you resize view back to larger size, you'll observe it jumping to the center of the view -
       * origin of QPainter is shifted.
       *
       * While QPainter passed to drawBackground() still has the right origin (see yellow rectangle which
       * is painted in drawBackground().
       */
      
      class MyScene : public QGraphicsScene
      {
      public:
          MyScene() {}
      
          void drawBackground(QPainter *p, const QRectF&)
          {
              QPixmap pix(40,40);
              pix.fill(Qt::yellow);
              p->drawPixmap(0, 0, pix);
          }
      
          void drawForeground(QPainter *p, const QRectF&)
          {
              QPixmap pix(40,40);
              pix.fill(Qt::green);
              p->drawPixmap(0, 0, pix);
          }
      
          void initItems()
          {
              setSceneRect(0, 0, 500,500);
              QPixmap pix(20,20);
              pix.fill(Qt::red);
              QGraphicsPixmapItem* item = new QGraphicsPixmapItem(0, this);
              item->setPixmap(pix);
      
              // THIS line causes QPainter translation in drawForeground() when view gets BIG.
              // And has no effect on QPainter (as it really should) when view size is small
      
              item->setPos(250,250);
              item->show();
          }
      };
      
      int main(int argc, char **argv)
      {
          QApplication a(argc,argv);
          MyScene* field = new MyScene();
      
          QGraphicsView* view = new QGraphicsView(field, 0);
      
          // Bug happens only when this optimization enabled (obviously)
          view->setOptimizationFlags( QGraphicsView::DontSavePainterState);
      
          view->show();
          view->resize(600,600);
          field->initItems();
      
          return a.exec();
      }
      

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

            bjnilsen Bjørn Erik Nilsen
            menard Alexis Menard (closed Nokia identity) (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes