Results 1 to 18 of 18

Thread: Scaling a pixmap in a label

  1. #1
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Post Scaling a pixmap in a label

    HI everyone,
    I'm new here and in programming Qt and I have a problem, so I hope you could explain it to me.
    Thank you very much.

    I have an image, represented by a pixmap, fit in a label in my mainwindow.
    I want to scale it but when I scale ithe pixmap, the label doesn't change, so I have two different situations:
    1) the label is the same but the pixmap inside it is scaled (so you can see only a part of the image)
    2) the label is the same and also the pixmap inside it

    In this code I have the second situation. Can you resolve this problem?
    Qt Code:
    1. void Interfaccia::zoomMappa()
    2. {
    3. QPixmap mappa("DefaultMap.png");
    4. ui->labelMap->setPixmap(mappa);
    5. ui->labelMap->setScaledContents(true);
    6. mappa.scaled(10,10,Qt::IgnoreAspectRatio);
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by GianluC93; 19th December 2015 at 13:40.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    scaled contents means the label will scale its content to fit the size of the label.

    So if the label stays the same size, then the content stays the same size.

    The last line in your function has no effect other than consuming CPU.
    It creates a scaled version of the pixmap which you then immediately discard.

    Cheers,
    _

  3. #3
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Scaling a pixmap in a label

    First of all, thank you for the reply.
    I've understood what you said about the last line and the rest.
    Can you tell me how can I scale the label to scale the pixmap inside it?
    Maybe if you could paste some code you would really help me to understand how to do it, because I'm not very able and even if I've understood I don't know how to do this.
    Thank you

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    If your label is in a layout, you will have to set the label's fixed size to whatever you want it to be and use scaled contents with that.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Scaling a pixmap in a label

    Yes but I have to scale it many times, so I cannot set a size for the label, because it could change many times.
    In fact this is only a first step just to understand how to scale it, because after I have to scale it connecting it to a vertical bar.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    I am afraid I don't understand.

    You need a label with a target size so you set that size.
    If the label has scaled contents set to true, it will scale its contents.

    Cheers,
    _

  7. #7
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Scaling a pixmap in a label

    Maybe I'm not understanding what you're saying.
    If you set the size of a label, the label will remain of that size?
    I think (if I have understood) that if I set a size for a label and after I scale the pixmap,
    the only thing that will scale will be the pixmap, not the label that has a fixed size. Am I wrong?
    So my question is:

    I have a mainwindow in which there are a label with a pixmap inside it, and near it a vertical bar that
    should allow to zoom the image.
    What should I do to scale my image when I 'move' (sorry, I don't know the correct word to explain it) the vertical bar?

    If you could paste some code maybe it would be easier to understand.
    Thank you

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    Quote Originally Posted by GianluC93 View Post
    If you set the size of a label, the label will remain of that size?
    If you set its fixed size, yes.-

    Quote Originally Posted by GianluC93 View Post
    I think (if I have understood) that if I set a size for a label and after I scale the pixmap,
    Why would you scale the pixmap?
    I thought you wanted the pixmap to be fully displayed.

    If you only want part of the image to be visible, then yes, scale the image, take the part you want to be visible, and let the label show it.

    Cheers,
    _

  9. #9
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Scaling a pixmap in a label

    Maybe it's better to simplify the question.
    I have an image (a pixmap in a label) and near it, a vertical bar. All of this is in a mainwindow.
    Can you tell me how can I do to zoom a little my image using the vertical bar?

    I don't want only a part of the image visibile, so (I suppose) I have to scale the image, but I don't know
    how to do it because everytime I do it the label (that is fixed) doesn't allow the image inside it to scale.
    So the image either remains of the same size (not scaling) or shows only a part of itself.
    I want to show it entirely, not fully displayed but only a little larger.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    If the image is to be shown entirely, the label needs to have a size big enough to show the whole image.

    In which case the easiest way is to resize the label appropriately and let it scale the pixmap.
    See comment #4

    Cheers,
    _

  11. #11
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Scaling a pixmap in a label

    Ok, so:

    1) if I fix a size big enough to contain the image larger, if the image is not zoomed in that moment there will be empty spaces between the label and the image?

    2)how can I connect this operation (scaling of the image) with the changes of the vertical bar?

    Thank you

  12. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    Quote Originally Posted by GianluC93 View Post
    1) if I fix a size big enough to contain the image larger, if the image is not zoomed in that moment there will be empty spaces between the label and the image?
    Why would you want to set a size larger than the image?

    Quote Originally Posted by GianluC93 View Post
    2)how can I connect this operation (scaling of the image) with the changes of the vertical bar?
    You connect to the bar's signal that indicates change of value and then calculate the target size.

    Cheers,
    _

  13. #13
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Scaling a pixmap in a label

    Because you wrote "If the image is to be shown entirely, the label needs to have a size big enough to show the whole image."
    I have to zoom it showing it entirely.

    Can you show me some code to understand how to do the second point (vertical bar)?

  14. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    Quote Originally Posted by GianluC93 View Post
    Because you wrote "If the image is to be shown entirely, the label needs to have a size big enough to show the whole image."
    I have to zoom it showing it entirely.
    Right, as big as necessary, not bigger :-)

    Quote Originally Posted by GianluC93 View Post
    Can you show me some code to understand how to do the second point (vertical bar)?
    Create a vertical QSlider, connect a slot to the slider's valueChanged() signal.

    Cheers,
    _

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Scaling a pixmap in a label

    I think we have a communication problem here. You are using the word "zoom". For us, to "zoom" an image means that you scale the image to make it larger ("zoom in") or smaller ("zoom out") than the rectangle it is displayed in.

    In most image display applications, the window stays the same size, so when you "zoom in" only part of the image is displayed, and usually scrollbars are shown so you can move to different parts of the image. When you "zoom out", more of the image is visible and as you zoom out more, the image becomes smaller than the window in which it is displayed and the scrollbars are removed. Zooming scales the image while the window size does not change.

    If you resize the window (by dragging the window to make it larger or smaller) the zoom level does not change, but more or less of the image is shown. Resizing the window does not scale the image.

    So, please explain exactly what you want to happen when you move your vertical bar:

    1) Do you want the window that the image is displayed in to get larger? (Resize the window, image stays the same size)
    2) Do you want the image to get larger or smaller? (Zoom the image, window stays the same size)
    3) Do you want both the window and the image to change size? (Zoom the image and at the same time make the window larger or smaller so the whole image is visible)

  16. #16
    Join Date
    Dec 2015
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Scaling a pixmap in a label

    Thank you d_stranz, maybe we have been a misunderstanding as you said.
    By the way, I partly solved the last problem so now I have a label with a fixed size and
    when I zoom in the image it becomes partially visible.
    How can I use the horizontal and vertical scrollbars in order to show the image completely?

    I have written the code "connect...ecc...." and the relative signals and slots but I cannot find
    a code for the function to connect to the scrollbars.

  17. #17
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Scaling a pixmap in a label

    Quote Originally Posted by GianluC93 View Post
    Thank you d_stranz, maybe we have been a misunderstanding as you said.
    Now that we all agree on that, could you please answer d_stranz' questions so that we may clear that up and move on? Because all I read are more confusing statements about "zooms" and "using scrollbars to show the image completely".

  18. #18
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Scaling a pixmap in a label

    How can I use the horizontal and vertical scrollbars in order to show the image completely?
    Look at the example in the QScrollArea widget. It does exactly what you are trying to do, and maybe you will look like the guy in the photo afterward.

Similar Threads

  1. Image/Pixmap after scaling parcelled into 4 pieces.
    By Douglish in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2011, 10:28
  2. Pixmap scaling problem
    By tommy in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2009, 18:00
  3. Replies: 8
    Last Post: 12th March 2009, 15:12
  4. finding maximum scaling of a pixmap
    By babu198649 in forum Newbie
    Replies: 1
    Last Post: 31st March 2008, 15:32
  5. Pixmap scaling
    By yellowmat in forum Newbie
    Replies: 3
    Last Post: 4th January 2007, 17:01

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.