Results 1 to 16 of 16

Thread: Zoom in/out in QImage

  1. #1
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Zoom in/out in QImage

    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?

  2. #2
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Re: Zoom in/out in QImage

    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!
    Last edited by TeresaML; 22nd November 2009 at 12:21.

  3. #3
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zoom in/out in QImage

    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.

  4. #4
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: Zoom in/out in QImage

    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.

  5. #5
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zoom in/out in QImage

    Quote Originally Posted by TeresaML View Post
    ...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?

  6. #6
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zoom in/out in QImage

    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

  7. #7
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Zoom in/out in QImage

    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:pen()
    {

    QString fileName=QFileDialog::getOpenFileName(this,tr("Ope n 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:aintEvent(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();
    }

  8. #8
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Zoom in/out in QImage

    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()));

  9. #9
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Zoom in/out in QImage

    Note that image.scaled() does not scale the image, it returns a scaled copy of it.

  10. #10
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Zoom in/out in QImage

    Could you give more details, please? any solution?

  11. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zoom in/out in QImage

    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

  12. #12
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Zoom in/out in QImage

    and never call update() in paintEvents ...

  13. #13
    Join Date
    Nov 2009
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Zoom in/out in QImage

    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...
    Last edited by TeresaML; 25th November 2009 at 11:23.

  14. #14
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Zoom in/out in QImage

    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.

  15. #15
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Zoom in/out in QImage

    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.

  16. #16
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Zoom in/out in QImage

    Addendum: The update() function never causes recursion according to Qt doc.
    Last edited by toutarrive; 10th March 2010 at 12:31.

Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. Web Page zoom in/out
    By abhilashajha in forum Qt Programming
    Replies: 1
    Last Post: 11th June 2009, 05:28
  3. QPainter doesn't seem to draw QImage
    By Denarius in forum Qt Programming
    Replies: 7
    Last Post: 3rd March 2009, 14:12
  4. QGLWidget with text - zoom in / zoom out
    By h123 in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2008, 09:56
  5. Zoom Options
    By Kapil in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2006, 11:19

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.