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

Rounding problem drawing on QGraphicsScene

    XMLWordPrintable

Details

    Description

      t4id: 222465

      Draw an item on QGraphicsScene mixing QPainter::drawLine(...) and QPainter::drawPath(...), and move it around, sometimes it will leave dots behind, and in some rare cases the horizontal lines are drawn with saw. It is suspected that rounding error has caused this problem. The dots can be reproduced on Linux and Windows, the line saw can be seen on Windows only.

      Update: As a workaround, you can either expand the bounding rect of the item, or reimplement the updateScene() slot in QGraphicsView and expand the update rectangles there.

      The following program reproduces this problem (the line saw can be rarely seen, you need to move the item slowly for many times to make it happen):

      item.h:

      #include <QGraphicsItem>
      
      class Capacitor: public QGraphicsItem
      {
          public:
              Capacitor(QGraphicsItem* parent=0):QGraphicsItem(parent){
                setFlag(QGraphicsItem::ItemIsMovable,true);
              }
              void paint ( QPainter * painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget = 0*/ )
              {
                  painter->drawLine(QLineF(-1,0,0,0));
                  painter->drawLine(QLineF(2,0,3.5,0));
                  painter->drawPath(capacitorPath());
              }
              QRectF boundingRect() const
              {
                QPointF p = pos();
                return QRectF(-1.75000004, -1.75000000004, 5.0 ,3.50000001);
              }
      
      
              QVariant itemChange(GraphicsItemChange change, const QVariant &value)
              {
                if (change == ItemPositionHasChanged && scene()) {
                  // value is the new position.
                  QPointF newPos = value.toPointF();
                  QPoint np = newPos.toPoint();
                  if ( QPointF(np) != newPos) {
                    setPos(np);
                  }
      
                }
                return QGraphicsItem::itemChange(change, value);
              }
      
          private:
      
              QPainterPath capacitorPath()
              {
                  QPainterPath p;
                  if ( p.isEmpty())
                  {
                      p.moveTo(0,0);
                      p.lineTo(0.75,0);
                      p.moveTo(0.75,-1);
                      p.lineTo(0.75,1);
                      p.moveTo(1.25,-1);
                      p.lineTo(1.25,1);
                      p.moveTo(1.25,0);
                      p.lineTo(2.0,0);
                  }
                  return p;
              }
      };
      

      main.cpp:

      #include <QtGui>
      #include <QApplication>
      #include <QDebug>
      #include <QGraphicsView>
      #include "item.h"
      
      int main(int argc, char** argv)
      {
          QApplication app(argc, argv);
          QGraphicsScene scene(QRectF(-200,-200,400,400));
          Capacitor c;
          scene.addItem(&c);
          QGraphicsView view(&scene);
          view.scale(7.07071,7.07071);
          view.show();
          return app.exec();
      }
      

      Attachments

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

        Activity

          People

            monsen Marius Bugge Monsen
            jasmcdon Jason McDonald (Closed Nokia Identity. Please assign to "macadder" instead) (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes