How to keep aspect ratio of QLabel after QPixmap?



  • 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



  • hi @jrod2much

    without regards on perfomance:

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


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



  • @jrod2much
    did you move pix.scaled outside the setPixmap call ? because scaled returns a new Scaled pixmap.



  • @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) );
    
    }
    


  • 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);
        
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.