Important: Please read the Qt Code of Conduct -

QScrollArea not expanding when QPixmap is loaded

  • Hi all,

    I'm working on a program that requires the user to load an image. I am using a QPixmap inside of a QLabel for the image, and that QLabel is inside a QScrollArea. When the program first starts, the label is empty, and thus the scroll area doesn't have anywhere to scroll (as intended). However, when I load an image that requires scrolling, the scroll bars do not change, and I can only see the top corner of the image.

    Currently, this is the code that sets the image in the QLabel (the image paths are stored in a list):

    void MainWindow::on_imageList_doubleClicked(const QModelIndex &index)
        QPixmap image(imageModel->itemData(index)[0].toString());

    As I said earlier, the QLabel (imagePanel) is inside the scroll area, so I am confused as to why the scroll bars do not expand to accommodate a large image.

    Any helps would be much appreciated.

    Edit: I admit that my explanation is a little confusing, so here's a screenshot showing what I mean. The picture is supposed to be scrollable so you can view the entire cat, but only a small part of the picture can be seen and the scroll bars do not work.
    I can somewhat get around the issue by setting the QLabel scaledContents to true, but it's not ideal as the height/width ratio is not maintained.

  • Moderators

    you need to resize the QLabel in the scrollarea to the size of the pixmap

  • Lifetime Qt Champion


    How did you set the QLabel on your QScrollArea ?

    If you put it in a layout on the scroll area then that's wrong.