Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 900
sandras robin Avatar asked Sep 19 '25 05:09

sandras robin


1 Answers

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)));
like image 148
G.M. Avatar answered Sep 21 '25 14:09

G.M.