qt-interest@trolltech.com
[Top] [All Lists]

Re: Questions on resizeEvent

Subject: Re: Questions on resizeEvent
From: Bo Thorsen
Date: Tue, 11 Sep 2007 08:49:11 +0200
On mandag den 10. September 2007, Lingfa Yang wrote:
> >>My real question is how to keep the image ratio during dragging.
> >>I use a scroll area to hold an image label. After
> >>    setWidgetResizable (true); // no side bars
> >>The images fit to the window, which can be at any ratio. Does anyone
> >>know how to keep the ratio and fit-to-window?
> >
> >The simplest way might be to write a custom widget which draws the
> >image scaled the way you want.
>
> J-P Nurmi,
>
> Thank you. I did this
> void ImageViewer1::resizeEvent ( QResizeEvent * event )
> {
>       QScrollArea::resizeEvent(event); //change viewport
>
>       QSize sise = event->size();
>       QSize oldSise = event->oldSize();
>
>       int dx = sise.width() - oldSise.width();
>       int dy = sise.height() - oldSise.height();
>
>       if(dx == 0) // drag along y
>               this->adjustWidth();
>       else
>               this->adjustHeight();
> }
> where,
> void ImageViewer1::adjustWidth()
> {
>       const QPixmap * pixmap = imageLabel->pixmap();
>       int w = this->size().height()*pixmap->width()/pixmap->height();
>       this->resize(w, this->size().height());
> }
>
> I am afraid call resize() within resizeEvent() will cause a dead-loop. But
> it it seems not. Qt's doc says: "Warning: Calling resize() or setGeometry()
> inside resizeEvent() can lead to infinite recursion. " What do you think?

I have done a resize inside a resizeEvent before. You just have to be 
absolutely certain that you at some point will stop this recursion. If you do 
that, there's no problem. However, you have to think about how hard this is 
going to be to maintain in 5 years. You're creating a timebomb here, that at 
the very least requires a big warning in comments.

Bo.


-- 

Thorsen Consulting ApS - Qt consulting services
http://www.thorsen-consulting.dk

--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>