Thalia Thalia - 3 months ago 13
C++ Question

How can I draw with pen width of 0 (invisible not cosmetic)?

I want to draw shapes with a border of various widths.

If I set width = 0 though, I expect (imagine) that here will be no border... yet I read that the border 0 is very thin ("cosmetic") border.

How can I draw my shapes with invisible border ?

int penWidth = 0; // some user input, 0 to 20 maybe
QPen pen = QPen(Qt::red, penWidth, Qt::PenStyle(Qt::SolidLine));
painter->setPen(pen);
painter->setBrush(QBrush(Qt::SolidPattern));
painter->drawRect(someRect);

Answer

You can simply set no pen, i.e.:

painter->setPen(Qt::NoPen);

In this case it will not draw a border line at all.