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. How to make a resizable image

How to make a resizable image

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 7.2k 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello, I added a QPixmap to a QLabel. But it must be set at a fixed size. Is there a way to resize it according to the size of the QLabel? I mean when I expand or shrink the QLabel with my mouse, I want that image resized simultaneously.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Sounds like you are looking for the QLabel:: scaledContents property.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Sounds like you are looking for the QLabel:: scaledContents property.

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @SGaist said in How to make a resizable image:

        Hi,

        Sounds like you are looking for the QLabel:: scaledContents property.

        No this is not what I want.
        Actually during my test, setScaledContents(true) only fits the image to the size of the label. When I magnify the label, the image doesn't change together with it.
        0_1523755942591_1.PNG
        1_1523755942592_2.PNG

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @MasterBlade said in How to make a resizable image:

          magnify the label,

          What do you do when you magnify it ?

          QLabel:: scaledContents scales the image to the size of Labels rect.

          alt text

          If you mean that the QLabel do not follow dialog, its because you must
          add a layout to the dialog and then label into layout to have it follow the Dialog window.

          That is, place Label on dialog form.
          Right click and select layout (on free spot on form, not label) ,
          select say layout horizontally
          Now it follows the Dialog.

          M 1 Reply Last reply
          4
          • mrjjM mrjj

            @MasterBlade said in How to make a resizable image:

            magnify the label,

            What do you do when you magnify it ?

            QLabel:: scaledContents scales the image to the size of Labels rect.

            alt text

            If you mean that the QLabel do not follow dialog, its because you must
            add a layout to the dialog and then label into layout to have it follow the Dialog window.

            That is, place Label on dialog form.
            Right click and select layout (on free spot on form, not label) ,
            select say layout horizontally
            Now it follows the Dialog.

            M Offline
            M Offline
            MasterBlade
            wrote on last edited by
            #5

            @mrjj said in How to make a resizable image:

            @MasterBlade said in How to make a resizable image:

            magnify the label,

            What do you do when you magnify it ?

            QLabel:: scaledContents scales the image to the size of Labels rect.

            alt text

            If you mean that the QLabel do not follow dialog, its because you must
            add a layout to the dialog and then label into layout to have it follow the Dialog window.

            That is, place Label on dialog form.
            Right click and select layout (on free spot on form, not label) ,
            select say layout horizontally
            Now it follows the Dialog.

            Wow, this did work! Many thanks!

            So the next problem is that the image doesn't maintain its aspect ratio when resizing. Is there a way to keep its aspect ratio?

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6
              • Is there a way to keep its aspect ratio?

              The most/one of the common solution is to subclass QLabel and
              use pixmap.scaled(w,h,Qt::KeepAspectRatio) and draw it by hand.

              You can grab AspectRatioPixmapLabel class from here

              https://stackoverflow.com/questions/8211982/qt-resizing-a-qlabel-containing-a-qpixmap-while-keeping-its-aspect-ratio

              M 1 Reply Last reply
              0
              • mrjjM mrjj
                • Is there a way to keep its aspect ratio?

                The most/one of the common solution is to subclass QLabel and
                use pixmap.scaled(w,h,Qt::KeepAspectRatio) and draw it by hand.

                You can grab AspectRatioPixmapLabel class from here

                https://stackoverflow.com/questions/8211982/qt-resizing-a-qlabel-containing-a-qpixmap-while-keeping-its-aspect-ratio

                M Offline
                M Offline
                MasterBlade
                wrote on last edited by MasterBlade
                #7

                @mrjj said in How to make a resizable image:

                • Is there a way to keep its aspect ratio?

                The most/one of the common solution is to subclass QLabel and
                use pixmap.scaled(w,h,Qt::KeepAspectRatio) and draw it by hand.

                You can grab AspectRatioPixmapLabel class from here

                https://stackoverflow.com/questions/8211982/qt-resizing-a-qlabel-containing-a-qpixmap-while-keeping-its-aspect-ratio

                Sorry I didn't go to this forum recently. This did work. Many thanks!!

                Allow me for one more stupid question. The image keeps it AR, but the window doesn't. Is there a way to make the window also keep its AR?

                I tried similar ways but it doesn't seem to be working.
                0_1524469567477_1.PNG

                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