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

QPainter::drawPolyline() uses wrong rounding for fractional coordinates

    XMLWordPrintable

Details

    Description

      #include <QWidget>
      #include <QPainter>
      
      class MyWidget : public QWidget
      {
          Q_OBJECT
      
      public:
          MyWidget(QWidget *parent = nullptr) : QWidget(parent) {}
      
      protected:
          void paintEvent(QPaintEvent*) override
          {
              QPainter painter(this);
              painter.setPen(QPen(Qt::black, 1));
              painter.drawPolyline( QPolygonF() << QPointF( 0, 0) << QPointF(10, 0) );
              painter.drawPolyline( QPolygonF() << QPointF(10, 1) << QPointF(20, 1) );
              painter.drawPolyline( QPolygonF() << QPointF( 0, 2) << QPointF(10, 2) );
              painter.drawPolyline( QPolygonF() << QPointF(10, 3) << QPointF(20, 3) );
      
              painter.setPen(QPen(Qt::green, 1));
              painter.drawPolyline( QPolygonF() << QPointF(10, 2.99) << QPointF(20, 2.99) );
          }
      };
      

      The attached screenshots are zoomed in 50x

      Expected behaviour

      The green line is drawn at y==3, always.

       

      Actual behaviour

      The green line is drawn at y==2 for all versions of Qt at scaling==100%

      The green line is drawn closer to y==3 for Qt 6 at scaling>=125%

      The green line is drawn closer to y==3 for Qt 5 at scaling>=200% (with Qt::AA_EnableHighDpiScaling)

      Attachments

        1. pixels1.png
          pixels1.png
          5 kB
        2. pixels2.png
          pixels2.png
          5 kB
        3. qpainter-rounding-actual-x50.png
          qpainter-rounding-actual-x50.png
          4 kB
        4. qpainter-rounding-expected-x50.png
          qpainter-rounding-expected-x50.png
          4 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vgt Eirik Aavitsland
            skoh-qt Sze Howe Koh
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes