Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Resizing QPixMap to fit QLabel issue

Resizing QPixMap to fit QLabel issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 13.8k Views
  • 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 Offline
    Q Offline
    Qtstarter121
    wrote on last edited by
    #1

    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?

    DiracsbracketD Venkatesh VV R 3 Replies Last reply
    2
    • Q Qtstarter121

      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?

      DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #2

      @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
      4
      • Q Qtstarter121

        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?

        Venkatesh VV Offline
        Venkatesh VV Offline
        Venkatesh V
        wrote on last edited by
        #3

        @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
        1
        • DiracsbracketD 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 Offline
          Q Offline
          Qtstarter121
          wrote on last edited by Qtstarter121
          #4

          @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.

          DiracsbracketD 1 Reply Last reply
          0
          • Q 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.

            DiracsbracketD Offline
            DiracsbracketD Offline
            Diracsbracket
            wrote on last edited by Diracsbracket
            #5

            @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
            0
            • Q Qtstarter121

              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?

              R Offline
              R Offline
              reza_sajadian
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved