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 is resizing when I set pixmap
Forum Updated to NodeBB v4.3 + New Features

QLabel is resizing when I set pixmap

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 13.6k Views 1 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.
  • U Offline
    U Offline
    umriyaev
    wrote on last edited by umriyaev
    #1

    Hi all,

    I have a QLabel which displays image frames as they arrive via socket. I'm using following code to scale the pixmap to label's size and set as pixmap:

    QPixmap pixmap = QPixmap::fromImage(image, Qt::ColorOnly);   
    qDebug()<< "Label size before setting a pixmap: \nWidth: " << ui->lblImage->width() << "; height: " << ui->lblImage->height();
     this->ui->lblImage->setPixmap(
                    pixmap.scaled(
                        ui->lblImage->size(),
                        Qt::IgnoreAspectRatio,
                        Qt::SmoothTransformation));
    qDebug()<< "Label size after setting a pixmap: \nWidth: " << ui->lblImage->width() << "; height: " << ui->lblImage->height();
    

    Here, lblImage is QLabel which is used to display my images.
    It's placed in the gridlayout, but I also tried vblayout and hblayout.

    The problem is, no matter which layout I use, my QLabel is growing everytime I set pixmap. However, debug output before and after setting pixmap shows same size, but next time when that slot is called the size is different.

    However, if I don't use scaled() method of QPixmap, the QLabel's size doesn't grow:

    this->ui->lblImage->setPixmap( pixmap);
    

    But in this case the image doesn't occupy the whole available space inside the QLabel.

    I tried to use following code before setting the pixmap

        this->ui->lblImage->setFixedWidth(image.width());
        this->ui->lblImage->setFixedHeight(image.height());
    

    And it solved the problem of QLabel's growing, however, QLabel doesn't resize when I resize the window.

    Btw, QLabel is inside the custom QWidget, that widget has other widgets too and they all are layed-out with gridlayout.
    And I didn't put my custom widget inside any QWindow or QDialog yet, just testing by calling my custom widget's show() method.

    So my question is how to make my label not grow when I set the pixmap? What is making it grow?

    raven-worxR 1 Reply Last reply
    0
    • U Offline
      U Offline
      umriyaev
      wrote on last edited by
      #3

      Thank you very much @raven-worx , it worked. :)

      What is the purpose of sizePolicy?

      raven-worxR 1 Reply Last reply
      0
      • U umriyaev

        Hi all,

        I have a QLabel which displays image frames as they arrive via socket. I'm using following code to scale the pixmap to label's size and set as pixmap:

        QPixmap pixmap = QPixmap::fromImage(image, Qt::ColorOnly);   
        qDebug()<< "Label size before setting a pixmap: \nWidth: " << ui->lblImage->width() << "; height: " << ui->lblImage->height();
         this->ui->lblImage->setPixmap(
                        pixmap.scaled(
                            ui->lblImage->size(),
                            Qt::IgnoreAspectRatio,
                            Qt::SmoothTransformation));
        qDebug()<< "Label size after setting a pixmap: \nWidth: " << ui->lblImage->width() << "; height: " << ui->lblImage->height();
        

        Here, lblImage is QLabel which is used to display my images.
        It's placed in the gridlayout, but I also tried vblayout and hblayout.

        The problem is, no matter which layout I use, my QLabel is growing everytime I set pixmap. However, debug output before and after setting pixmap shows same size, but next time when that slot is called the size is different.

        However, if I don't use scaled() method of QPixmap, the QLabel's size doesn't grow:

        this->ui->lblImage->setPixmap( pixmap);
        

        But in this case the image doesn't occupy the whole available space inside the QLabel.

        I tried to use following code before setting the pixmap

            this->ui->lblImage->setFixedWidth(image.width());
            this->ui->lblImage->setFixedHeight(image.height());
        

        And it solved the problem of QLabel's growing, however, QLabel doesn't resize when I resize the window.

        Btw, QLabel is inside the custom QWidget, that widget has other widgets too and they all are layed-out with gridlayout.
        And I didn't put my custom widget inside any QWindow or QDialog yet, just testing by calling my custom widget's show() method.

        So my question is how to make my label not grow when I set the pixmap? What is making it grow?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #2

        @umriyaev said in QLabel is resizing when I set pixmap:

        So my question is how to make my label not grow when I set the pixmap? What is making it grow?

        The reason is it's sizeHint, which gets updated when you set a pixmap and notifies the layout about it.
        You can try to set the size policy of the label:

        label->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored );
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        1
        • U Offline
          U Offline
          umriyaev
          wrote on last edited by
          #3

          Thank you very much @raven-worx , it worked. :)

          What is the purpose of sizePolicy?

          raven-worxR 1 Reply Last reply
          0
          • U umriyaev

            Thank you very much @raven-worx , it worked. :)

            What is the purpose of sizePolicy?

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #4

            @umriyaev said in QLabel is resizing when I set pixmap:

            What is the purpose of sizePolicy?

            see the docs

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            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