Qt QLabel fails to scale/resize



  • 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.



  • 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.



  • 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.



  • 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.



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



  • 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.



  • 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()).



  • 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...


Log in to reply
 

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