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

Drawing is inaccurate with different pens, when drawing a roated QGraphicsViewItem

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Out of scope
    • Icon: P3: Somewhat important P3: Somewhat important
    • 4.6.0
    • 4.3.2
    • GUI: Painting
    • None

      When roatating a QGraphicsViewItem, and then drawing a path in the paint functions, the path is drawn inaccurately. A solid pen and a dashed pen does not draw themselves at the exact overlapping position.

      On Mac with Qt 4.3.0 it paints correctly.
      On Mac with the current Qt 4.3 snapshot, it is inaccuratley painted for 90 and 270 degrees. The solid box is bigger than the same box drawn with a dashed pattern. Since it is bigger the two boxes don't overlap as expected.

      It should also be verified that this works on Windows and X11. This is also reported to be a problem on other platforms.

      Reproducible with the following example:

      #include <QApplication>
      #include <QPen>
      #include <QBrush>
      #include <QGraphicsScene>
      #include <QGraphicsView>
      #include <QGraphicsTextItem>

      class SomeItem : public QGraphicsItem
      {
      public:
      SomeItem();
      virtual ~SomeItem();
      QRectF boundingRect() const;
      void set_rotation(int rotation);
      void paint(QPainter* painter, const QStyleOptionGraphicsItem *gi,
      QWidget *wid);
      static QVector<qreal> ant_dashes;
      };

      QVector<qreal> SomeItem::ant_dashes = QVector<qreal>();

      SomeItem::SomeItem()
      : QGraphicsItem()
      {
      ant_dashes << 3 << 3;
      }

      SomeItem::~SomeItem()
      {
      }

      void SomeItem::set_rotation(int angle)
      {
      rotate(static_cast<qreal>(angle));
      }

      QRectF SomeItem::boundingRect() const
      {
      QRectF br(-20.0, -3.562178, 23.5, 15.124356);
      return br;
      }

      void SomeItem::paint(QPainter* painter, const QStyleOptionGraphicsItem *gi,
      QWidget *wid)
      {

      QPainterPath wid_shape = shape();
      int pen_width = 1;
      qreal half_pen_width = 1.0;

      // Adjust the size of the shape so that drawing the box will stay
      // within the bounding rect.
      QRectF br = wid_shape.boundingRect();

      QRectF adjusted_br(br.x(), br.y(),
      br.width() - half_pen_width,
      br.height() - half_pen_width);

      QPainterPath adjusted_wid_shape;
      adjusted_wid_shape.addRect( adjusted_br );

      painter->save();

      QColor dash_color("red");
      QColor space_color("yellow");

      QPen pen = painter->pen();
      pen.setStyle(Qt::SolidLine);
      pen.setWidth(pen_width);
      pen.setCapStyle(Qt::FlatCap);

      // Draw the background of the box, the space_color.
      pen.setColor(space_color);
      painter->setPen(pen);
      painter->drawPath(adjusted_wid_shape);

      // Draw the foreground of the box with the dashes, the dash_color.
      pen.setColor(dash_color);
      pen.setDashPattern(ant_dashes);
      painter->setPen(pen);
      painter->drawPath(adjusted_wid_shape);

      painter->restore();
      }

      int main( int argc, char *argv[] )
      {
      QApplication app( argc, argv );
      QGraphicsScene scene;
      scene.setSceneRect( 0, 0, 600, 600 );
      // Scene background is grey
      scene.setBackgroundBrush( QColor("grey") );

      // This item is not rotated
      SomeItem some_item_0;
      some_item_0.setPos(50.0, 50.0);

      // This item is rotation 90 degrees
      SomeItem some_item_90;
      some_item_90.setPos(100.0, 50.0);
      some_item_90.rotate(90);

      // This item is rotation 180 degrees
      SomeItem some_item_180;
      some_item_180.setPos(150.0, 50.0);
      some_item_180.rotate(180);

      // This item is rotation 270 degrees
      SomeItem some_item_270;
      some_item_270.setPos(200.0, 50.0);
      some_item_270.rotate(270);

      // This item is rotation 45 degrees
      SomeItem some_item_45;
      some_item_45.setPos(250.0, 50.0);
      some_item_45.rotate(45);

      scene.addItem( &some_item_0 );
      scene.addItem( &some_item_90 );
      scene.addItem( &some_item_180 );
      scene.addItem( &some_item_270 );
      scene.addItem( &some_item_45 );

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

      return app.exec( );
      };

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

            rodal Samuel Rødal
            admin Administrator
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes