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. Maintaining aspect ratio of qlabel
Qt 6.11 is out! See what's new in the release blog

Maintaining aspect ratio of qlabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 4 Posters 8.1k Views 3 Watching
  • 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.
  • J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #7

    you can override paintEvent and do it yourself:

    for example (assuming its a square image)

    void myLabel::paintEvent( QPaintEvent* event )
    {
         int min = qMin(height(), width());
         int xOff = (width() - min) / 2;
         int yOff = (height() - min) / 2;
    
        QPainter p(this);
        p.drawImage(QRect(xOff,yOff,min,min), m_myImage);
    }
    

    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    KiraK 1 Reply Last reply
    1
    • J.HilkJ J.Hilk

      you can override paintEvent and do it yourself:

      for example (assuming its a square image)

      void myLabel::paintEvent( QPaintEvent* event )
      {
           int min = qMin(height(), width());
           int xOff = (width() - min) / 2;
           int yOff = (height() - min) / 2;
      
          QPainter p(this);
          p.drawImage(QRect(xOff,yOff,min,min), m_myImage);
      }
      
      KiraK Offline
      KiraK Offline
      Kira
      wrote on last edited by
      #8

      @J-Hilk : Thanks for the reply:
      Current I am using the following:

      void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
      {
          QLabel::resizeEvent(pQEvent);
          setPixmap(_qPixmap, pQEvent->size());
      }
      
      void LabelAnnotation::setPixmap(const QPixmap &qPixmap, const QSize &size)
      {
          QPixmap _qPixmapScaled;
          _qPixmap = qPixmap;
          _qPixmapScaled = _qPixmap.scaled(size, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
          QLabel::setPixmap(_qPixmapScaled);
      }
      

      Will I have to remove the above function if i override paintEvent as mentioned ?

      J.HilkJ 1 Reply Last reply
      1
      • KiraK Kira

        @J-Hilk : Thanks for the reply:
        Current I am using the following:

        void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
        {
            QLabel::resizeEvent(pQEvent);
            setPixmap(_qPixmap, pQEvent->size());
        }
        
        void LabelAnnotation::setPixmap(const QPixmap &qPixmap, const QSize &size)
        {
            QPixmap _qPixmapScaled;
            _qPixmap = qPixmap;
            _qPixmapScaled = _qPixmap.scaled(size, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
            QLabel::setPixmap(_qPixmapScaled);
        }
        

        Will I have to remove the above function if i override paintEvent as mentioned ?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by J.Hilk
        #9

        @Kira I don't think so,

        you can access your modified/scaled pixmap by calling pixmap() and QPainter has a drawPixmap method as well.


        just noticed, that what I wrote, does not actually match what you want.

        Do I understand it correctly, that you want a way to rescale your QLabel, so that it fits the Pixmap ?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        KiraK 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @Kira I don't think so,

          you can access your modified/scaled pixmap by calling pixmap() and QPainter has a drawPixmap method as well.


          just noticed, that what I wrote, does not actually match what you want.

          Do I understand it correctly, that you want a way to rescale your QLabel, so that it fits the Pixmap ?

          KiraK Offline
          KiraK Offline
          Kira
          wrote on last edited by
          #10

          @J-Hilk : yes
          I tried running your code its shrinks my pixmap a bit :p

          J.HilkJ 1 Reply Last reply
          0
          • KiraK Offline
            KiraK Offline
            Kira
            wrote on last edited by
            #11

            @jsulm : Just a strange thing the program crashes after some time without throwing any specific error

            1 Reply Last reply
            0
            • KiraK Kira

              @J-Hilk : yes
              I tried running your code its shrinks my pixmap a bit :p

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #12

              @Kira well, is your label part of a layout or do you resize it manually?

              because you will have to resize it manually, having it in a layout will make things tricky


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              KiraK 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @Kira well, is your label part of a layout or do you resize it manually?

                because you will have to resize it manually, having it in a layout will make things tricky

                KiraK Offline
                KiraK Offline
                Kira
                wrote on last edited by
                #13

                @J-Hilk : My label is part of the Grid layout.
                I have seen various articles related to the issue, but none of them provide an exact solution. Is there a specific way to handle such a scenario.

                J.HilkJ 1 Reply Last reply
                0
                • KiraK Kira

                  @J-Hilk : My label is part of the Grid layout.
                  I have seen various articles related to the issue, but none of them provide an exact solution. Is there a specific way to handle such a scenario.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #14

                  @Kira
                  Ok, I suggest the following, I used it before with moderate success rate.

                  set the SizePolcy of either width or height to fixed.

                  inside the overloaded resizeEvent, set the min width/height of your fixed side to the appropriate length (according to the pixmap ratio)

                  It should work, but be beware of potential recursive resizeEvents, as this is a clunky workaround.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  KiraK 1 Reply Last reply
                  0
                  • J.HilkJ J.Hilk

                    @Kira
                    Ok, I suggest the following, I used it before with moderate success rate.

                    set the SizePolcy of either width or height to fixed.

                    inside the overloaded resizeEvent, set the min width/height of your fixed side to the appropriate length (according to the pixmap ratio)

                    It should work, but be beware of potential recursive resizeEvents, as this is a clunky workaround.

                    KiraK Offline
                    KiraK Offline
                    Kira
                    wrote on last edited by
                    #15

                    @J-Hilk :
                    OK, thanks, understood.
                    What does moderate success mean?
                    How can I determine the aspect ratio ?
                    I will figure the code but would be grateful if you have any working sample.

                    J.HilkJ 1 Reply Last reply
                    0
                    • KiraK Kira

                      @J-Hilk :
                      OK, thanks, understood.
                      What does moderate success mean?
                      How can I determine the aspect ratio ?
                      I will figure the code but would be grateful if you have any working sample.

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #16

                      @Kira

                      What does moderate success mean

                      when actively resizing the window, you may get "flickering" as the new min size may tricker a new recalculating of the other widgets inside the layout

                      How can I determine the aspect ratio
                      I will figure the code but would be grateful if you have any working sample

                      untested:

                      //Assuming horizontal size policy is fixed
                      void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
                      {
                          double minw = (static_cast<double>(_qPixmap.width()) * height()) / _qPixmap.height();
                         setMinimumWidth(static_cast<int>(minw));
                      }
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      KiraK 3 Replies Last reply
                      2
                      • J.HilkJ J.Hilk

                        @Kira

                        What does moderate success mean

                        when actively resizing the window, you may get "flickering" as the new min size may tricker a new recalculating of the other widgets inside the layout

                        How can I determine the aspect ratio
                        I will figure the code but would be grateful if you have any working sample

                        untested:

                        //Assuming horizontal size policy is fixed
                        void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
                        {
                            double minw = (static_cast<double>(_qPixmap.width()) * height()) / _qPixmap.height();
                           setMinimumWidth(static_cast<int>(minw));
                        }
                        
                        KiraK Offline
                        KiraK Offline
                        Kira
                        wrote on last edited by
                        #17

                        @J-Hilk :Thanks a lot will implement it and let you know.

                        1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @Kira

                          What does moderate success mean

                          when actively resizing the window, you may get "flickering" as the new min size may tricker a new recalculating of the other widgets inside the layout

                          How can I determine the aspect ratio
                          I will figure the code but would be grateful if you have any working sample

                          untested:

                          //Assuming horizontal size policy is fixed
                          void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
                          {
                              double minw = (static_cast<double>(_qPixmap.width()) * height()) / _qPixmap.height();
                             setMinimumWidth(static_cast<int>(minw));
                          }
                          
                          KiraK Offline
                          KiraK Offline
                          Kira
                          wrote on last edited by
                          #18

                          @J-Hilk : Here are we resizing the window or the QLabel.
                          I implemented the following, but still, there is space left when I make the window to full screen.

                          J.HilkJ 1 Reply Last reply
                          0
                          • KiraK Kira

                            @J-Hilk : Here are we resizing the window or the QLabel.
                            I implemented the following, but still, there is space left when I make the window to full screen.

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #19

                            @Kira said in Maintaining aspect ratio of qlabel:

                            I implemented the following

                            I don't see it. May be lost in the depth of the forum ?


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            0
                            • J.HilkJ J.Hilk

                              @Kira

                              What does moderate success mean

                              when actively resizing the window, you may get "flickering" as the new min size may tricker a new recalculating of the other widgets inside the layout

                              How can I determine the aspect ratio
                              I will figure the code but would be grateful if you have any working sample

                              untested:

                              //Assuming horizontal size policy is fixed
                              void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
                              {
                                  double minw = (static_cast<double>(_qPixmap.width()) * height()) / _qPixmap.height();
                                 setMinimumWidth(static_cast<int>(minw));
                              }
                              
                              KiraK Offline
                              KiraK Offline
                              Kira
                              wrote on last edited by
                              #20

                              @J-Hilk said in Maintaining aspect ratio of qlabel:

                              void LabelAnnotation::resizeEvent(QResizeEvent *pQEvent)
                              {
                              double minw = (static_cast<double>(_qPixmap.width()) * height()) / _qPixmap.height();
                              setMinimumWidth(static_cast<int>(minw));
                              }

                              This one :)

                              1 Reply Last reply
                              0
                              • J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #21

                                can you show us a screenshot of the image inside the layout, it's still difficult for me to visualize what the issue is exactly ;)


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                KiraK 1 Reply Last reply
                                1
                                • J.HilkJ J.Hilk

                                  can you show us a screenshot of the image inside the layout, it's still difficult for me to visualize what the issue is exactly ;)

                                  KiraK Offline
                                  KiraK Offline
                                  Kira
                                  wrote on last edited by
                                  #22

                                  @J-Hilk : Sure

                                  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