View Full Version : problem about drawRect

22nd April 2008, 05:06
I don't understand about drawRect. Please see this example:

void MyWidget:: paintEvent(QPaintEvent *e)
QPainter p(this);
QRect r = QRect(100, 100, 400, 400);

int x1, y1, x2, y2);
r.getCoords(&x1, &y1, &x2, &y2); // it will return x1=100, y1=100, x2=499, y2=499
p.drawLine(x1, y1, x2, y1);
p.drawLine(x2, y1, x2, y2);
p.drawLine(x1, y2, x2, y2);
p.drawLine(x1, y1, x1, y2);

Please tell me why 2 rectangle not overlap? I really not understand about the meaning of drawRect function.

22nd April 2008, 08:02
Are you asking why there is 499 instead of 500 or why there is 499 instead of 400?

27th April 2008, 03:04
I understand why 499 instead of 500. My problem is that why the function drawRect don't draw a rectangle (100, 100, 400, 400) begin from (100, 100) to (499, 499), but in this case this rectangle draw from (100, 100) to (500, 500).

Because I always think both of solutions will give 2 rectangles overlap together.

27th April 2008, 12:10
Could you restate the problem? What do you mean by "overlap"? That they intersect or that they have exactly the same coordinates or all four corners?

will draw a rectangle beginning in point (100,100) and having a width of 400 and height of 400. So the bottom right corner will have coordinates of (499,499). drawLine() and drawRect() may interpret the pen width differently (I'm not sure of that, but it's a good guess the rect version would draw it's line half the pen width to the inside (left and up in this case) than drawLine).

29th April 2008, 02:07
Good. Thank you too much.
But why drawLine() and drawRect() may interpret the pen width differently?