PDA

View Full Version : scale painter to fit my widget



jesse_mark
4th June 2013, 21:36
hello ,

Im trying to paint a rect in paintEven,
but the rect I draw has a definided values for hieght and widt, which could be really big comparing to the widget.

so i want to painter

- to scale the rect based on the widget size, how can i do that ??
- as well as i want my (0,0) to be in the bottom left of the widget not in the top left.

I have attache an example of what i have trying to and the result i got.

i also tried to do zooming and panning on it.

Thanks for any help

here an example of the code im doing :
9099

wysota
4th June 2013, 22:48
Have a look at QPainter::setWindow() and QPainter::setViewport().

jesse_mark
4th June 2013, 23:20
by looking into setWindow and setViewPort,

I understood that by setWindow i can tell the painter the rect i want it to draw in it.
so i did this :



QRect widgetRect = this->rect();
QRect windowRect;

windowRect.setHeight(widgetRect.height()-50);
windowRect.setWidth(widgetRect.width()-50);
windowRect.setX(widgetRect.x()-50);
windowRect.setY(widgetRect.y()-50);

painter.setWindow(windowRect);
painter.drawRect(myRect);



expecting that when i draw using the painter it will draw inside this windowRect.

but it does not, it will draw out of the border in the invisable area for the user.

I dont know what exactlly i should set it.

wysota
5th June 2013, 08:05
setWindow() defines a logical rectangle the painter will use when accepting coordinates from you. setViewport() defines a physical rectangle the logical rectangle will be mapped to. So if you pass setViewport(widget->rect()) and say... setWindow(QRect(0,0,100,50)) then 100 will be scaled to the width of your widget and 50 to the height of your widget.

jesse_mark
5th June 2013, 19:11
Thanks , this is what i was looking for.
but now im facing other problem, which is the size.

for example,

I want to draw an ellipce in the same area.

i want the postion of the ellipce to be affected and to be mapped to viewport area. but i want it to keep the same size i assinge to it.

for example :

Painter.drawEllpice(x,y,2,2);
now when i map it to the viewPort, and when the x,y are really big, you can not see the ellipce at all and as size is mapped as well and beome sooo small.

so is there a way to can keep the size always the same no matter what x,y is or no matter what viewport i gave.

Thanks

wysota
5th June 2013, 19:42
Did you use setWindow?

jesse_mark
5th June 2013, 20:51
yes I did.

this is wat i did:
Qrect my rect(x,y,h,w);
QPoint p(x1,y1);

painter.setViewPort(widget->rect());
painter.setwindow(x,y,h,w);


painter.drawRect(myrect);
painter.drawEllipce(p,2,2);


is my rect is small , the ellipce is fine, but if the rect is big and then the ellipce is sooo samll that u cannot see
i have to set the size of the ellipce so big so i can see it.

like:
painter.drawEllipce(p,2000,2000);

but ofcource then is the window is small the ellipce will be too big.

wysota
5th June 2013, 20:59
What did you pass as x, y, h and w? (btw. it is w and h and not h and w)

jesse_mark
5th June 2013, 22:14
i passed the value of my rect:

for example :



QRect myrect(0,0,2000,4000);
QPoint(1000,2000);

QRect veiwRect = widget->rect();


painter.setViewPort(viewRect)
painter.setWindow(myRect);

painter.drawRect(myRect);


I notice something else is that, it will fill the whole widget and it will not use its a coordinates, so even if was the height a way bigger than the width,
it still show that the width bigger than the height.

wysota
5th June 2013, 23:55
If you pass such big values then no wonder your ellipse is so small. 2 / 2000 = 0.001; 2 / 4000 = 0.0005 so your ellipse takes 0.1% x 0.05% of the widget area.

jesse_mark
6th June 2013, 00:26
yeah i know, so this is why im asking if there is a way to make the size of the ellipce fixed, because the rect some times have big values and other may have small value.

so im asking if there is a way to make the ellipce has fixed size and will be showen with same size all the times.

wysota
6th June 2013, 00:30
yeah i know, so this is why im asking if there is a way to make the size of the ellipce fixed

Draw the ellipse before you use setWindow() and setViewport() or pass invalid rects to the two to reset them to default before painting the ellipse.

jesse_mark
6th June 2013, 01:09
the issue is that the ellipse position then.
where the position of the ellipse is related to the rect i draw.
if i do it before or pass empty rects, the shot will be drawn out side of the visible area of the widget.

wysota
6th June 2013, 01:17
I'm sorry, I do not understand what you want. Unless you manage to explain it better, I'm afraid I can't help you.

jesse_mark
6th June 2013, 02:22
OK, ill try to explain what im trying to do exactly,

I have a small widget, lets say it with size of 250, 250.

I want to draw a rectangle and an ellipse.

the rect can be really big or small depend on the user input. and the ellipse position as well.
i want them to be scaled to fit in the widget.

ps: the ellipse position will be really close to the rect, so it could be beside one of the rect borders or in the middle of the rect.

wysota
6th June 2013, 09:04
It is still not clear what you want. For me "scaled to fit" means the ellipse and rectangle should be the size of the widget:


void Widget::paintEvent(QPaintEvent *pe) {
QPainter p(this);
p.setBrush(Qt::red);
p.drawRect(rect());
p.setBrush(Qt::yellow);
p.drawEllipse(rect());
}

If you wish to keep the aspect ratio then compare width to height and scale the rectangle to the smaller of the two values.


int side = qMin(width(), height());
QRect r(0,0, side, side);
r.moveCenter(rect().center());
p.drawRect(r);
p.drawEllipse(r);

jesse_mark
6th June 2013, 17:32
Thank you for your help and for your time.
sorry for not being clear of what im trying to do exactlly.



Thanks.

wysota
6th June 2013, 17:39
And where is "scaled to fit" in all this?

jesse_mark
6th June 2013, 18:03
I used setviewPort and setWindow , as you told me, to fit in the widget, if i dont use that the rect will not show in my widget.
and still is not showen as i want it to.


The rect size can be realy big and the ellipse postion can be any in the middel of the rect of outside it as in the graph.

the code i attached does not give the result i want.