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. QLabel with image stretched out
QtWS25 Last Chance

QLabel with image stretched out

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt designerpython
9 Posts 4 Posters 514 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.
  • R Offline
    R Offline
    Rangerguy128
    wrote on 3 Aug 2024, 17:45 last edited by Rangerguy128 8 Mar 2024, 17:47
    #1

    Been designing a window for bactria analysis and one of the main functions I want to add is the option to upload an image with the help of QT Designer. However, when I maximize the window, the image looks too stretched out. Anyway to fix this? Here's what it looks like:
    73ec0a30-039e-4076-8144-0c0a8b8fbbcb-image.png
    The image is shown through QLabel. Here are the objects for additional detail:
    ffcc96ee-ec34-461b-b035-4401c8923eb2-image.png

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 3 Aug 2024, 17:47 last edited by
      #2

      See https://doc.qt.io/qt-6/qlabel.html#scaledContents-prop

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply 3 Aug 2024, 17:50
      0
      • C Christian Ehrlicher
        3 Aug 2024, 17:47

        See https://doc.qt.io/qt-6/qlabel.html#scaledContents-prop

        R Offline
        R Offline
        Rangerguy128
        wrote on 3 Aug 2024, 17:50 last edited by
        #3

        @Christian-Ehrlicher I disabled it (as it was enabled before) but I'm still getting the same issue unfortunately: e377b1a4-92f0-4b7c-9a64-71d59fc45274-image.png

        C 1 Reply Last reply 4 Aug 2024, 07:30
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 3 Aug 2024, 17:56 last edited by
          #4

          If you disable scaled contents then your image will not be scaled. And your picture shows that now the pixmap is no longer scaled to the width/height of your label.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          R 1 Reply Last reply 4 Aug 2024, 17:22
          1
          • R Rangerguy128
            3 Aug 2024, 17:50

            @Christian-Ehrlicher I disabled it (as it was enabled before) but I'm still getting the same issue unfortunately: e377b1a4-92f0-4b7c-9a64-71d59fc45274-image.png

            C Offline
            C Offline
            ChrisW67
            wrote on 4 Aug 2024, 07:30 last edited by
            #5

            I am assuming you were expecting that the image would be pillar-boxed i.e. scaled to the height of the client area, without changing aspect ratio, and placed in the centre.

            One way to achieve this is to keep a copy of the original QPixmap and scale it as needed in the relevant QWidget::resizeEvent(). You can see the logic in the Screenshot Example

            void Screenshot::resizeEvent(QResizeEvent * /* event */)
            {
                QSize scaledSize = originalPixmap.size();
                scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
                if (scaledSize != screenshotLabel->pixmap().size())
                    updateScreenshotLabel();
            }
            
            R 1 Reply Last reply 4 Aug 2024, 17:14
            2
            • C ChrisW67
              4 Aug 2024, 07:30

              I am assuming you were expecting that the image would be pillar-boxed i.e. scaled to the height of the client area, without changing aspect ratio, and placed in the centre.

              One way to achieve this is to keep a copy of the original QPixmap and scale it as needed in the relevant QWidget::resizeEvent(). You can see the logic in the Screenshot Example

              void Screenshot::resizeEvent(QResizeEvent * /* event */)
              {
                  QSize scaledSize = originalPixmap.size();
                  scaledSize.scale(screenshotLabel->size(), Qt::KeepAspectRatio);
                  if (scaledSize != screenshotLabel->pixmap().size())
                      updateScreenshotLabel();
              }
              
              R Offline
              R Offline
              Rangerguy128
              wrote on 4 Aug 2024, 17:14 last edited by
              #6

              @ChrisW67 I'm currently usiing Python to run this window by converting the qt file into a py file. How can I implement it with said language?

              1 Reply Last reply
              0
              • C Christian Ehrlicher
                3 Aug 2024, 17:56

                If you disable scaled contents then your image will not be scaled. And your picture shows that now the pixmap is no longer scaled to the width/height of your label.

                R Offline
                R Offline
                Rangerguy128
                wrote on 4 Aug 2024, 17:22 last edited by
                #7

                @Christian-Ehrlicher I see. Is there anyway to have the image remain in normal size even when the window is maximized? Should I include the Qlabel inside a widget for it to work?
                2bfd6edd-6fe2-4510-ae46-d470d575141b-image.png

                J 1 Reply Last reply 4 Aug 2024, 17:38
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 4 Aug 2024, 17:25 last edited by
                  #8

                  What is 'normal size'? If you want to make sure that all is drawn without stretching then use @ChrisW67 's solution.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • R Rangerguy128
                    4 Aug 2024, 17:22

                    @Christian-Ehrlicher I see. Is there anyway to have the image remain in normal size even when the window is maximized? Should I include the Qlabel inside a widget for it to work?
                    2bfd6edd-6fe2-4510-ae46-d470d575141b-image.png

                    J Online
                    J Online
                    JonB
                    wrote on 4 Aug 2024, 17:38 last edited by JonB 8 Apr 2024, 17:39
                    #9

                    @Rangerguy128
                    As @Christian-Ehrlicher says we don't know what "normal" size means to you. If you mean you have an original image file with a certain size picture in it and you don't want the user to resize it bigger than this you could set the maximum size to the original size. You were asking about resizing originally, now you don't want it to resize, resizing when small but not too large, what precisely do you want or not want?

                    1 Reply Last reply
                    0

                    1/9

                    3 Aug 2024, 17:45

                    • Login

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