Results 1 to 19 of 19

Thread: Zooming image

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Zooming image

    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.

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zooming image

    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
    Last edited by JohannesMunk; 17th September 2010 at 17:27.

  3. The following user says thank you to JohannesMunk for this useful post:

    MTK358 (17th September 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    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:

    Qt Code:
    1. void MainWindow::fitButton(bool on) // note that fitButton is checkable
    2. {
    3. ui->imageScrollArea->setWidgetResizable(on);
    4. ui->actualSizeButton->setDisabled(on);
    5. ui->zoomSlider->setDisabled(on);
    6. ui->zoomLabel->setDisabled(on);
    7. }
    8.  
    9. void MainWindow::actualSizeButton()
    10. {
    11. ui->zoomSlider->setValue(100);
    12. }
    13.  
    14. void MainWindow::zoomSlider(int value)
    15. {
    16. QSize size = ui->imageLabel->pixmap()->size();
    17. size.setWidth((size.width() * value) / 100);
    18. size.setHeight((size.height() * value) / 100);
    19. ui->imageLabel->resize(size);
    20. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zooming image

    http://www.qtcentre.org/threads/3105...map-in-a-label

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

    Joh

  6. #5
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    Good idea, it will solve both the speed and aspect ratio problems. I'm going to implement it now.

  7. #6
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    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.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Zooming image

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    The problem is that I'm using a Designer form. How would I put my own widget right in the middle of a layout?

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Zooming image

    Quote Originally Posted by MTK358 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    MTK358 (18th September 2010)

  12. #10
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    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.

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Zooming image

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #12
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    OK, I looked but I don't understand what methods to reimplement and how to paint to the viewport.

  15. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Zooming image

    Quote Originally Posted by MTK358 View Post
    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().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #14
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    OK, but how do I get a QPainter for the viewport widget?

  17. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Zooming image

    You create it in the paint event like usual.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #16
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    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?

  19. #17
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

    You're gradually reinventing QGraphicsView here. Just use that; you get all of the features you've brought up so far for free.

  20. #18
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zooming image

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

  21. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Zooming image

    Quote Originally Posted by MTK358 View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. smooth panning and zooming an image, to use OpenGL or not?
    By scarleton in forum Qt Programming
    Replies: 0
    Last Post: 27th August 2010, 23:25
  2. Center based image zooming
    By plagoon in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2010, 17:09
  3. Image Zooming
    By navi1084 in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2008, 13:48
  4. Clever image zooming. What to write in PaintEvent?
    By ModeZt in forum Qt Programming
    Replies: 8
    Last Post: 16th December 2007, 14:39
  5. Replies: 3
    Last Post: 11th March 2007, 14:04

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.