PDA

View Full Version : Problem understanding QPainter::setWindow (maybe)



EMKAH
14th July 2012, 10:08
Hello everybody.

i have a quite simple problem but couldn't get along with it.

In the documentation, I've read that this code:



QPainter p(this);
...
p.setWindow(-50,-50,100,100);


would create a painting environment with (0, 0) as the center. But if I try to paint an ellipse which should fill out the widget completely with the following code:



p.drawEllipse(-50,-50,100,100);


I get the following result:

8014

So, obviously, the center is not (0, 0) but (-50, -50)?

I if draw the ellipse with:


p.drawEllipse(-100,-100,100,100);

I (almost) get the result i want to have:

8015

Well, this is not the coordinate system I expected. :rolleyes: (0, 0) should be the center of my painting environment. What's my fault?

Greetings,
EMKAH

wysota
14th July 2012, 12:43
Why do your drawings cover only part of the window? Do you have one widget embedded in another? Are you using layouts? How do you know the boundaries of the internal widget? The initial code is fine and should draw a perfect ellipse covering the whole area of the widget.

EMKAH
14th July 2012, 13:48
Actually, i'm drawing on the widget itself. I've set the viewport to the largest square that fits into the widget:


int _shortSide = qMin(width(), height());
QPainter p;
p.begin(this);
p.setViewport((width() - _shortSide / 2), (height() - _shortSide / 2),
_shortSide, _shortSide);
p.setWindow(-50, -50, 100, 100);


The reason because I want to have 0,0 as the center of my coordinate system is that i want to perform a rotation around the center for further drawings.

wysota
14th July 2012, 14:25
If the drawing is cut then your call to setViewport() is apparently invalid.

However if you want to do rotations then I doubt setWindow is a good solution. It's easier to just translate the painter.


void X::paintEvent(QPaintEvent *) {
QPainter p(this);
p.translate(width()/2, height()/2);
p.drawEllipse(-50, -50, 100, 100);
}