Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Resizing QPixMap to fit QLabel issue

    General and Desktop
    4
    6
    8889
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Q
      Qtstarter121 last edited by

      Re: Scaling QPixmap to fit Label

      So I have been trying to do this multiple ways,

      So far I have

      label->setPixmap(pixmap);
      label->setScaledContents(true);
      

      Now the above does scale the image so that it fits, however the problem is that the image looks all compressed and ugly. Is there maybe some better format other than QPixmap, or some way to fix the image so it looks good?

      Diracsbracket Venkatesh V R 3 Replies Last reply Reply Quote 2
      • Diracsbracket
        Diracsbracket @Qtstarter121 last edited by Diracsbracket

        @Qtstarter121

        If you know the size of the label on beforehand, and if it is fixed, then you could simply use:

        label->setPixmap(pixmap.scaled(myWidth, myHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation));
        

        or with Qt::IgnoreAspectRatio.

        In any case, drop the setScaledContents(true); part.

        If your label size varies dynamically, e.g. as part of a layout when resizing the window, then you must subclass QLabel to implement the resizeEvent handler, as done here:
        https://stackoverflow.com/questions/8211982/qt-resizing-a-qlabel-containing-a-qpixmap-while-keeping-its-aspect-ratio

        Q 1 Reply Last reply Reply Quote 4
        • Venkatesh V
          Venkatesh V @Qtstarter121 last edited by

          @Qtstarter121 said in Resizing QPixMap to fit QLabel issue:

          label->setScaledContents(true);

          instead of this, scale the pixmap to labels width and height

          pixmap.scaled(label.width(),label.height());

          1 Reply Last reply Reply Quote 1
          • Q
            Qtstarter121 @Diracsbracket last edited by Qtstarter121

            @Diracsbracket The label size does vary dynamically. I set a scaled pixmap like you wrote and also included the same line in my event filter ( on resize event ) for my class but it didn't scale properly. I know for a fact that my label's height should have changed but the height of the pixmap didn't. However if I implement the same line with

            Qt::IgnoreAspectRatio
            

            Then it produces the same result I observed with just :

            label->setPixmap(pixmap);
            label->setScaledContents(true);
            

            And like I said, it's scaled, but looks ugly as it is way too compressed horizontally.

            I'm thinking maybe this has something to do with the original size format of my pixmap and maybe it just won't scale the way I want it to unless I recreate it with a better size in mind - although not sure how that would work.

            Diracsbracket 1 Reply Last reply Reply Quote 0
            • Diracsbracket
              Diracsbracket @Qtstarter121 last edited by Diracsbracket

              @Qtstarter121 said in Resizing QPixMap to fit QLabel issue:

              I'm thinking maybe this has something to do with the original size format of my pixmap

              What is the unscaled pixel size of your pixmap?
              Can you post a minimal runnable code version of how you are currently doing things?

              1 Reply Last reply Reply Quote 0
              • R
                reza_sajadian @Qtstarter121 last edited by

                @Qtstarter121 the setScaledContents(True) worked perfectly for me on python. thanks.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post