[Development] Issues with QPainter::drawPolygon (off by one error?)
Allan Sandfeld Jensen
kde at carewolf.com
Sun Apr 28 13:50:30 CEST 2024
On Friday 26 April 2024 21:52:48 CEST Christian Ehrlicher via Development
wrote:
> Hello,
>
> I'm currently trying to investigate a painting problem within the
> windowsvista / common style:
>
> QImage img(7, 7, QImage::Format_ARGB32_Premultiplied);
> QPolygon poly{ QPoint(1, 1), {5, 1}, {3, 5} };
> QPainter p(&img);
> p.setPen(Qt::NoPen);
> p.setBrush(QColor(Qt::black));
> p.drawPolygon(poly);
> p.setPen(QColor(0, 255, 0, 128));
> p.drawPoints(poly.data(), poly.size());
> p.end();
> img.save("output.png");
>
> This code should draw a small down arrow (e.g. for a QPushButton with a
> QMenu attached) and should look similar to this:
>
> 0123456
> 0
> 1 XXXXX
> 2 XXX
> 3 XXX
> 4 X
> 5 X
> 6
>
> But the outcome is this:
>
> 0123456
> 0
> 1 XXXX
> 2 XX
> 3 XX
> 4
> 5
> 6
>
> Setting a non-cosmetic pen at least result in painting of all three
> corners but gives a non-symmetric triangle:
>
> 0123456
> 0
> 1 XXXXX
> 2 XXXX
> 3 XXX
> 4 XX
> 5 X
> 6
>
I think the problem with the cosmetic pen is partly the need to be symmetric.
Years ago I tried cleaning up the cosmetic pen, but had to revert it because
it violated rules about symmetry.
https://codereview.qt-project.org/c/qt/qtbase/+/200383
Though perhaps the non-cosmetic painting could have the rounding adjusted so
it would look better for your example. Otherwise try offsetting the triangle
by 0.5
Best regards
Allan
More information about the Development
mailing list