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 keep aspect ratio of QLabel after QPixmap?

How to keep aspect ratio of QLabel after QPixmap?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 11.7k 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.
  • J Offline
    J Offline
    jrod2much
    wrote on last edited by
    #1

    Hello,
    I have a Qlabel that I load an image to and I have read a lot of forum posts but can't seem to keep the aspect ratio. and one time I was successful but it ruined the resolution of the image. How can i do this.

    void MainWindow::chromaticity() {
      int h = m_ui->label_3->height();
      int w = m_ui->label_3->width();
      QPixmap pix(w, h);
      m_ui->label_3->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
      pix.load(":/icons/icons/chromeL.png");
    
    
      QPainter paint(&pix);
    
    
    
      paint.setPen(QColor(0, 0, 0, 255));
      int y = 0;
      int x = 0;
      int bw = 10; // bar width
      for (int barcount = 0; barcount < 50; ++barcount) {
        paint.setBrush(QColor(255, 0, 255, 255));
        paint.drawRect(78+x, 825,  4, 4 );
        x += bw + 3;
      }
      paint.end();
      m_ui->label_3->setPixmap(pix);
    }
    

    0_1540453564856_d94a425f-79a1-47c8-821e-3cd54d5dcf74-image.png

    J.HilkJ 1 Reply Last reply
    0
    • J jrod2much

      Hello,
      I have a Qlabel that I load an image to and I have read a lot of forum posts but can't seem to keep the aspect ratio. and one time I was successful but it ruined the resolution of the image. How can i do this.

      void MainWindow::chromaticity() {
        int h = m_ui->label_3->height();
        int w = m_ui->label_3->width();
        QPixmap pix(w, h);
        m_ui->label_3->setPixmap(pix.scaled(w,h,Qt::KeepAspectRatio));
        pix.load(":/icons/icons/chromeL.png");
      
      
        QPainter paint(&pix);
      
      
      
        paint.setPen(QColor(0, 0, 0, 255));
        int y = 0;
        int x = 0;
        int bw = 10; // bar width
        for (int barcount = 0; barcount < 50; ++barcount) {
          paint.setBrush(QColor(255, 0, 255, 255));
          paint.drawRect(78+x, 825,  4, 4 );
          x += bw + 3;
        }
        paint.end();
        m_ui->label_3->setPixmap(pix);
      }
      

      0_1540453564856_d94a425f-79a1-47c8-821e-3cd54d5dcf74-image.png

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

      hi @jrod2much

      without regards on perfomance:

      .....
       m_ui->label_3->setPixmap(pix.scaled(  m_ui->label_3->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
      

      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 Offline
        J Offline
        jrod2much
        wrote on last edited by
        #3

        @J-Hilk
        It isn't working, where do I put it in my code? Maybe Im placing it in the wrong place.

        J.HilkJ 1 Reply Last reply
        0
        • J jrod2much

          @J-Hilk
          It isn't working, where do I put it in my code? Maybe Im placing it in the wrong place.

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

          @jrod2much
          did you move pix.scaled outside the setPixmap call ? because scaled returns a new Scaled 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.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jrod2much
            wrote on last edited by
            #5

            @J-Hilk I know exactly what you are saying but my friend is pretty dumb, maybe you could explain it for them. Thanks

            void MainWindow::chromaticity() {
                int h = m_ui->label_3->height();
                int w = m_ui->label_3->width();
                QPixmap pix(":/icons/icons/chromeL.png");
                m_ui->label_3->setPixmap(pix.scaled(  m_ui->label_3->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
            
                if(chromeX != 0 && chromeX != 0)
                {
                    QPainter paint(&pix);
                    paint.setPen(QColor(0, 0, 0, 255));
                    paint.setBrush(QColor(255, 255, 255, 255));
                    paint.drawRect(chromeX - 4, chromeY- 4,  8, 8);
                    paint.end();
                }
                m_ui->label_3->setPixmap(pix.scaled(  m_ui->label_3->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
            
            }
            
            J.HilkJ 1 Reply Last reply
            0
            • J jrod2much

              @J-Hilk I know exactly what you are saying but my friend is pretty dumb, maybe you could explain it for them. Thanks

              void MainWindow::chromaticity() {
                  int h = m_ui->label_3->height();
                  int w = m_ui->label_3->width();
                  QPixmap pix(":/icons/icons/chromeL.png");
                  m_ui->label_3->setPixmap(pix.scaled(  m_ui->label_3->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
              
                  if(chromeX != 0 && chromeX != 0)
                  {
                      QPainter paint(&pix);
                      paint.setPen(QColor(0, 0, 0, 255));
                      paint.setBrush(QColor(255, 255, 255, 255));
                      paint.drawRect(chromeX - 4, chromeY- 4,  8, 8);
                      paint.end();
                  }
                  m_ui->label_3->setPixmap(pix.scaled(  m_ui->label_3->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
              
              }
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              alright, imagine the following, I'm using qDebug() to print some numbers.

                 qDebug() << "Size of the Label" << m_ui->label_3->size();
              
                  QPixmap pix(":/icons/icons/chromeL.png");
              
                  qDebug() << "Size of the original Image" << pix.size();
              
                  QPixmap pixScaled = pix.scaled(  m_ui->label_3->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
                  //Either pixScaled.width() == label_3->width() or pixScaled.height() == label_3->height() with the companion parameter beeing smaller to guaranty aspec-ratio
              
                  qDebug() << "Size of the scaled Pixmap" << pixScaled.size();
                  m_ui->label_3->setPixmap(pixScaled);
                  
              

              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
              1

              • Login

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