PDA

View Full Version : Using negative values for the x and y args to QRectF when calling Qpainter.drawElipse



edmhourigan
27th December 2020, 14:11
What is the purpose of passing negative values to the QRect constructor for the x and y parameters?

Example:



QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, antialiased);
painter.translate(width() / 2, height() / 2);

for (int diameter = 0; diameter < 256; diameter += 9)
{
painter.drawEllipse(QRectF(-diameter / 2.0, -diameter / 2.0, diameter, diameter));
}



The code above draws a series of concentric circles centered at a fixed point. If I change to positive values, the center of each circle moves, drawing a 'cone effect' on the screen.



My question is what does negating the x and y arguments really do?
I have searched a lot for this topic but nothing comes up that really answers my question.

Thanks.

d_stranz
27th December 2020, 17:23
My question is what does negating the x and y arguments really do?

The center of the circle drawn by drawEllipse() is the center of rectangle you give it as argument. So if you want every circle drawn around the same center, then you have to use that same center point every time. The rectangle is defined by its top left (x,y) origin, width and height, not its center. So if you increase the diameter of the circle without moving the top left corner of the rectangle to compensate, then the center of the rectangle will move to the right with each call, and your circles will make a cone shape.

By subtracting half the diameter from x and y with each pass through the loop, you are giving it the same center each time so the circles remain concentric. You can easily confirm this - make the QRectF a local variable inside the loop and print out its center() using qDebug() in the debugger.

edmhourigan
27th December 2020, 18:58
Ah, ok, that explains it. Yes, I first thought the x and y to QRectF was the center of the circle, not its top-left corner. Thanks!