[Development] Issues with QPainter::drawPolygon (off by one error?)

Henry Skoglund henry at tungware.se
Sun Apr 28 11:39:42 CEST 2024



On 2024-04-28 11:00, Christian Ehrlicher via Development wrote:
>
> Am 26.04.2024 um 22:48 schrieb Henry Skoglund:
>> On 2024-04-26 21:52, 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");
>>> ...
>>>
>> Hi, I think the QPainter draws very poorly using direct polygon paths.
>> You could try going via a QPainterPath, say:
>>
>>     QImage img(7, 7, QImage::Format_ARGB32_Premultiplied);
>>     img.fill(QColor(0, 255, 0, 128));    // to be sure the img
>> contains no junk values
>>     QPolygon poly{ QPoint(1, 1), {5, 1}, {3, 5} };
>>     QPainter p(&img);
>>     p.setPen(Qt::NoPen);
>>     p.setBrush(QColor(Qt::black));
>>     p.setPen(QColor(0, 255, 0, 128));
>>     QPainterPath path;
>>     path.addPolygon(poly);
>>     p.drawPath(path);
>>     p.end();
>>     img.save("output.png");
>>
>> Rgrds Henry
>>
>
> This helped a little bit but it then fails again for other sizes. It's a
> mess :(
> I'm shortly before drawing the triangles pixel by pixel by myself...
>
If you close the polygon by adding the first point again, it will help 
to draw a better triangle:
     QImage img(7, 7, QImage::Format_ARGB32_Premultiplied);
     img.fill("white");    // clear the scene
     QPolygon poly{ QPoint(1, 1), {5, 1}, {3, 5}, {1, 1} };  // added a 
point
     QPainter p(&img);
     p.setPen(Qt::NoPen);
     p.setBrush(QColor(Qt::black));
     p.setPen(QColor(0, 255, 0, 128));
     QPainterPath path;
     path.addPolygon(poly);
     p.drawPath(path);
     p.end();
     img.save("output.png");



More information about the Development mailing list