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->setScaledContents() does not work
Forum Update on Monday, May 27th 2025

QLabel->setScaledContents() does not work

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

    Hello friends, I have the following code:

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QMessageBox>
    #include <QFileDialog>
    Dialog::Dialog(QWidget *parent)
      : QDialog(parent)
      , ui(new Ui::Dialog)
    {
      ui->setupUi(this);
    
    }
    
    Dialog::~Dialog()
    {
      delete ui;
    }
    
    
    void Dialog::on_toolButton_clicked()
    {
      QString fileName=QFileDialog::getOpenFileName(this,"Abrir imagenes",
                                                    QDir::rootPath(),"Imagenes (*.jpg *.jpeg *.png)");
      if(fileName.isEmpty())
        {
          return;
        }
      ui->label->setPixmap(QPixmap(fileName));
      ui->label->setScaledContents(true);
    
    }
    

    what I want is that when loading an image in the QLabel it adjusts to the size of the control but it doesn't work, I already put it in the constructor but it didn't work either; I tried to mark the property in the graphic designer but nothing, I would like to know why it is not working.

    Solitary wolf

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2
      #include <QtWidgets>
      int main(int argc, char **argv)
      {
          QApplication app(argc, argv);
      
          QString fileName=QFileDialog::getOpenFileName(nullptr, QString(), QString(), "Imagenes (*.jpg *.jpeg *.png)");
          QLabel lbl;
          lbl.setPixmap(QPixmap(fileName));
          lbl.setScaledContents(true);
          lbl.show();
      
          return app.exec();
      }
      

      This is working fine for me - an image set on the label is scaled and resized when I resize the label.

      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
      1
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        My guess is that it is because the OP is trying to modify the pixmap in a label managed from within a dialog. The dialog is probably setting some constraints that override the setScaledContent() call.

        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