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. Qt QLabel fails to scale/resize
Forum Updated to NodeBB v4.3 + New Features

Qt QLabel fails to scale/resize

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 13.1k 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.
  • D Offline
    D Offline
    deguolao
    wrote on last edited by
    #1

    I implemented QLabel much like Qt's ImageViewer example, except I use QGridLayout for positioning my widgets. I also implemented similar lines for scaling my QLabel using QScrollBar, but QLabel just doesn't scale like it should inside the QScrollArea. I am not sure if it is related to some kind of GridLayout management issue or something else. I have been reading everywhere and trying different things for 3 days now. Below I list the relevant portion of my code.

    In my Viewer class constructor:
    @{
    imageLabel1 = new QLabel;
    imageLabel1->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    imageLabel1->setScaledContents(true);

    scrollArea1 = new QScrollArea;
    scrollArea1->setWidget(imageLabel1);
    scrollArea1->setWidgetResizable(true);

    ....
    QGridLayout *centralLayout = new QGridLayout;
    centralLayout->addWidget(scrollArea1, 0, 0);
    ...}@

    and my scaleImage method:

    @void Viewer::scaleImage1(int factor)
    {
    Q_ASSERT(imageLabel1->pixmap());
    scaleFactor = (1 + factor);
    //imageLabel1->resize(scaleFactor
    imageLabel1->pixmap()->size());

    imageLabel1->pixmap()->toImage().scaled(scaleFactor* imageLabel1->pixmap()->size(), Qt::KeepAspectRatio, Qt::FastTransformation);
    imageLabel1->adjustSize();

    adjustScrollBar(scrollArea1->horizontalScrollBar(), factor);
    adjustScrollBar(scrollArea1->verticalScrollBar(), factor);

    imageLabel1->update();
    }@

    I'd appreciate your advice/tips very much.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      bq. QLabel just doesn’t scale like it should inside the QScrollArea.

      What are you expecting it to do and what is it actually doing?

      The size of the widget inside the QScrollArea is unrelated to the layout containing the QScrollArea itself or the size of the scroll area view port. You possibly want a QSizePolicy::Fixed on the image label.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deguolao
        wrote on last edited by
        #3

        I forgot to mention that my scaleImage1 function is a public slot, and it receives signal from a scrollbar that goes between 0 and 2 so that, into the scaleFactor, the imageLabel1 is designed to be capable of being zoomed in up to 3 times its original size. But when I run the code, I don't observe the imageLabel becoming enlarged inside the QScrollArea, which I saw in the imageViewer demo. The imageLabel1 simply retains the original size as it is loaded and does not respond to the valueChange() of scrollbar. I tried changing the QSizePolicy to Fixed, and the situation stays the same.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          Hi,
          Did you try with a different layout ? Check if you are setting the layout correctly -
          setLayout(centralLayout). It will be helpful if you can provide the complete implementation or a test implementation to generate the same behaviour.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            At line 7 in scaleImage1() you are:

            • accessing the label QPixmap,
            • creating a copy of the pixmap converted to a temporary QImage,
            • scaling that temporary QImage and ignoring the returned scaled QImage, and
            • then discarding the temporary QImage.

            At the end of this the QLabel, I expect, is unchanged.

            Try combining QPixmap::scaled() and QLabel::setPixmap()

            1 Reply Last reply
            0
            • D Offline
              D Offline
              deguolao
              wrote on last edited by
              #6

              thanks, Chris!

              I have a feeling that your comment is pointing to a promising direction. I changed my scaleImage1 function to the following, combining QLabel::setPixmap and QPixmap::scaled, but still the QLabel does not change in size...

              @void regiView5::scaleImage1(int factor)
              {
              Q_ASSERT(imageLabel1->pixmap());
              scaleFactor = (1 + factor);
              //imageLabel1->resize(scaleFactor
              imageLabel1->pixmap()->size());

              imageLabel1->setPixmap(imageLabel1->pixmap()->scaled(scaleFactor* imageLabel1->pixmap()->size(), Qt::KeepAspectRatio, Qt::FastTransformation));

              imageLabel1->adjustSize();

              adjustScrollBar(scrollArea1->horizontalScrollBar(), factor);
              adjustScrollBar(scrollArea1->verticalScrollBar(), factor);
              
              imageLabel1->update();
              

              }@

              The commented-out line above where it starts with imageLabel1->resize is taken from the imageViewer demo, should enable zooming, but it also doesn't do anything.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                I suggest you revert the scaling code to that in the Image Viewer example: I know that works. Pay special attention to the image label size policy in the example.

                Make sure you actually apply the grid layout containing the scroll area to the widget that contains it (setLayout()).

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  deguolao
                  wrote on last edited by
                  #8

                  I use a centralWidget
                  @centralWidget = new QWidget;
                  setCentralWidget(centralWidget);@

                  and because scrollArea1 includes imageLabel1, so I write
                  @QGridLayout *centralLayout = new QGridLayout;
                  centralLayout->addWidget(scrollArea1, 0, 0);
                  ....
                  centralWidget->setLayout(centralLayout);@

                  But in scaleImage1 function, imageLabel1-> resize does not seem to execute...

                  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