PDA

View Full Version : Zooming image



MTK358
17th September 2010, 16:12
I have a QLabel (displaying an image) inside a QScrollArea. How do I find out by how much to scale the image to make it fit with no scrollbars? There will also be a zoom slider to adjust it manually.

You can tell me is using QGraphicsView is a better idea, I'm just not familiar with it.

JohannesMunk
17th September 2010, 16:21
Have you tried QLabel::setScaledContents(true);?

I didn't get the Scrollarea at first! There is an example that does just what you want:

http://doc.qt.nokia.com/latest/widgets-imageviewer.html

Joh

MTK358
17th September 2010, 20:55
That's a nice link, but I still have problems:

Scrollbars don't appear when the image is larger than the viewport.

Switching back to fit mode doesn't work.

The image's aspect ratio isn't retained in fit mode.

It uses a smooth scaling algorithm which takes a ridiculously long time to complete.

Here is some relavent code:


void MainWindow::fitButton(bool on) // note that fitButton is checkable
{
ui->imageScrollArea->setWidgetResizable(on);
ui->actualSizeButton->setDisabled(on);
ui->zoomSlider->setDisabled(on);
ui->zoomLabel->setDisabled(on);
}

void MainWindow::actualSizeButton()
{
ui->zoomSlider->setValue(100);
}

void MainWindow::zoomSlider(int value)
{
QSize size = ui->imageLabel->pixmap()->size();
size.setWidth((size.width() * value) / 100);
size.setHeight((size.height() * value) / 100);
ui->imageLabel->resize(size);
}

JohannesMunk
17th September 2010, 21:28
http://www.qtcentre.org/threads/31058-Change-the-size-of-pixmap-in-a-label

Subclass QLabel. And handle the resizeEvent yourself with fast transformation turned on.

Joh

MTK358
17th September 2010, 21:47
Good idea, it will solve both the speed and aspect ratio problems. I'm going to implement it now.

MTK358
17th September 2010, 21:54
Wait a minute — what do I do in the resizeEvent()? Where to store the resized image?

Also, it should still handle text, for "file format not supported" messages and stuff like that.

wysota
17th September 2010, 22:08
I would subclass QAbstractScrollArea and implement everything as a single custom widget. Placing a QLabel in a QScrollArea will cause you problems sooner or later.

MTK358
18th September 2010, 01:13
The problem is that I'm using a Designer form. How would I put my own widget right in the middle of a layout?

wysota
18th September 2010, 07:40
How would I put my own widget right in the middle of a layout?
Use the "Promote to..." feature of Designer - put a QFrame on the form, right click the frame, choose "Promote to..." and enter your custom widget's data.

MTK358
18th September 2010, 13:39
The Promote feature is great! I didn't know about it.

But OK, I created a custom widget containing the scroll area, label, and zoom controls, but it still has the same issues. It just behaves very strangely and so inconsistently that I can't really even explain it.

wysota
18th September 2010, 19:33
I told you to subclass QAbstractScrollArea and implement the functionality from scratch, not to use a custom widget composed of a scroll area and a label. You will have to get your hands dirty here.

MTK358
18th September 2010, 21:10
OK, I looked but I don't understand what methods to reimplement and how to paint to the viewport.

wysota
18th September 2010, 21:30
OK, I looked but I don't understand what methods to reimplement and how to paint to the viewport.

Reimplement at least paintEvent() and optionally scrollContentsBy().

MTK358
18th September 2010, 22:20
OK, but how do I get a QPainter for the viewport widget?

wysota
18th September 2010, 22:45
You create it in the paint event like usual.

MTK358
27th September 2010, 14:34
Finally, I did it.

Anyway, it works great now, but I would really like it so that the user can scroll by dragging the mouse in the viewport. How would I do that?

SixDegrees
27th September 2010, 14:47
You're gradually reinventing QGraphicsView here. Just use that; you get all of the features you've brought up so far for free.

MTK358
27th September 2010, 16:34
You're gradually reinventing QGraphicsView here. Just use that; you get all of the features you've brought up so far for free.

I'm not very familiar with QGraphicsView but maybe...

Anyway, why didn't anyone else offer that?

wysota
27th September 2010, 18:25
Anyway, why didn't anyone else offer that?
I'm myself against using Graphics View for displaying a single image. In my opinion it gives too much overhead without giving anything else in return.