PDA

View Full Version : Zoom in/out in QImage



TeresaML
21st November 2009, 19:14
Hi,

I'm doing a GUI to visualize images and then, to operate with them. I'm doing it with QDialog and QPushButtons, mainly.

I load an image file and represent it by means of QImage and QPaintEvent in two diferents frames. I'm using QPaintEvent for representing the loaded image and this function is adjusting the image size in the frame. In the second one I want to zoom in/out the image. I've tried to do this with "image.scaled()" in a created function zoomin/out that it's called when you click a button in the Dialog, but nothing happens with it.

I think it happens because when QPaintEvent is called, it uses the same image as that loaded at the beginning, and any change is observed.

What change should I do to make a zoom in the loaded image?

TeresaML
22nd November 2009, 11:21
I can see some people have read my previous message but they have not answered. Is there any detail I'm forgotting to get an answer to my problem? Any help is very welcome. Thanks!

ecanela
23rd November 2009, 07:04
hi.

please see the ImageViewer in the section of Widgets example. the example show how display an image, and provide very basics zooming and scaling function.

the link -- http://doc.trolltech.com/4.5/widgets-imageviewer.html

hope you find this useful.

TeresaML
23rd November 2009, 12:07
Thank very much you for your answer. I had already seen this page and I think that I don't use this example as reference but since it uses QMainWindow and QPixmap, and I use QDialog and QImage, so it's a little bit different to implement. I think it is not interesting to me...

Thank you again for the answer, and as I have said before, any help is welcome.

scascio
23rd November 2009, 14:09
...I've tried to do this with "image.scaled()" in a created function zoomin/out that it's called when you click a button in the Dialog, but nothing happens with it.

What instruction on QPainter are you employing to draw the image?
Depending on the way you draw, maybe scaling the image has no effect.

Did you try to scale the painter or the viewport?

ecanela
24th November 2009, 04:41
QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen.

why need show a QImage?, when QPixmap is better choice for you needs.

please post some source code, maybe i am wrong and QImage is a better choice in your case

TeresaML
24th November 2009, 09:03
Hi,

I've created a function called "open()" for opening an image file and loading in a two differents frames (one for representing the original image and the other one for making some changes in it, like scale it).

void GUI::open()
{

QString fileName=QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty())
{
ui.frame->image.load(fileName);
ui.frame_2->image.load(fileName);
}
}

For representing the images I've used a paintEvent function:

MiQFrame::MiQFrame(QWidget *parent) : QFrame(parent)
{

}
void MiQFrame::paintEvent(QPaintEvent *event)
{
QPainter* p;
p = new QPainter(this);
image = image.scaled(this->size());
p->drawImage(QPoint(0,0),image);
update();

}

At last, for scaling the second image represented in the second frame:

void GUI::zoomin()
{
ui.frame_2->image.scaled(120,120);
update();
}

TeresaML
24th November 2009, 09:08
I forgot to comment that I've used QPushButtons:

QObject::connect(openButton, SIGNAL(clicked()), GUIClass, SLOT(open()));
QObject::connect(zoominButton, SIGNAL(clicked()), open2Class, SLOT(zoomin()));

drhex
24th November 2009, 22:42
Note that image.scaled() does not scale the image, it returns a scaled copy of it.

TeresaML
24th November 2009, 22:49
Could you give more details, please? any solution?

aamer4yu
25th November 2009, 04:23
soemthing like -
scaledImage = image.scaled(200,200); // scaled returns a copy which you need to assign. It wont alter the original image.

Hope you get the point :)

nish
25th November 2009, 09:23
and never call update() in paintEvents ...:)

TeresaML
25th November 2009, 11:13
What does it happen when update() is used in PainEvent?

In my case, I've detected that if I don't put Update() image isn't represented in the frame...

toutarrive
25th November 2009, 23:13
There is no need to call update() in an Paint Event .
The paintEvent() method is called automatically when:

Your widget is shown for the first time.
After a window has been moved to reveal some part (or all) of the widget.
The window in which the widget lies is restored after being minimized.
The window in which the widget lies is resized.
The user switches from another desktop to the desktop on which the widget's window lies.

What you can do from the paint event is to call a member function which will do the scaling of your image and will return the image in the paint event.

toutarrive
25th November 2009, 23:27
Update() or repaint() are used to generate a paint event.
Calling those function inside the paint event itself can introduce a problem of endless painting recursivity.

toutarrive
10th March 2010, 12:18
Addendum: The update() function never causes recursion according to Qt doc.