sandras robin
sandras robin

Reputation: 51

Undesirable black border on my rect when i draw it with QPainter.drawRect()

I have an undesirable black line of one or two pixels on the top of my rect when i use drawRect(). So my rect doesn't fill my widget completely.

My code :

QPainter Painter(this);

QString TmpColor;

int R, G, B, A;

TmpColor = c_LabColor;

R = TmpColor.left(TmpColor.indexOf(',')).toInt();
TmpColor.remove(0, TmpColor.indexOf(',') + 1);
G = TmpColor.left(TmpColor.indexOf(',')).toInt();
TmpColor.remove(0, TmpColor.indexOf(',') + 1);
B = TmpColor.left(TmpColor.indexOf(',')).toInt();
TmpColor.remove(0, TmpColor.indexOf(',') + 1);
A = TmpColor.left(TmpColor.indexOf(',')).toInt();

Painter.setBrush(QBrush(QColor(R,G,B,A)));
Painter.drawRect(this->rect());

Thank you for your suggestions.

Upvotes: 4

Views: 1820

Answers (1)

G.M.
G.M.

Reputation: 12879

QPainter::drawRect uses the current pen to draw the rectangle outline and fills the rectangle using the current pen. Since you don't explicitly set the pen it will be the default which is probably black and 1 pixel wide. Hence the border.

If all you want to do is fill the widget rectangle completely then just use one of the QPainter::fillRect overloads instead...

Painter.fillRect(rect(), QBrush(QColor(R,G,B,A)));

Upvotes: 5

Related Questions