Important: Please read the Qt Code of Conduct -

Help Please! Problem with QScrollArea and QLabel image inside

  • Hello everyone,

    I am having a lot of trouble with this, please help me.

    I am following the Image Viewer example, but I am using Designer.
    I have a MainWindow, inside a QScrollArea and inside a QLabel to show an image.
    My GUI looks like this:


    And I have a zoom action.

    My files are:


    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    private slots:
    void zoom();

    Ui::MainWindow *ui;

    #endif // MAINWINDOW_H@

    and mainwindow.cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QImage>

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    QImage img("C:/Qt/Programas/PruebaScrollArea/Desert.jpg");
    connect(ui->actionZoom, SIGNAL(triggered()), this, SLOT(zoom()));


    delete ui;

    void MainWindow::zoom()


    When I run the program, the image show fine, with the scrollbars correctly like the following image:


    When I maximize, the image shows like this, with the scrollbars correctly disabled:


    But when I zoom to double the image size, the image seems to move and nothing happens to the scrollbars:


    What is happening there? I´been struggling with this and I don´t know why it doesn´t work well.

    In scrollarea I have widgerResizable to true and in label I have the scaleContents false.

    I´ll appreciate your help.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should rather scale the pixmap rather than resize the label so you'd be working directly with the image data.

    Hope it helps

  • Hi,

    Thanks for your answer.

    Isn´t this line:


    doing that?

    Shouldn´t I resize the label that contains the pixmap so the pixmap itself can fit inside?

    I mean, I don´t exactly get what you mean, but in the Image Viewer example, they resize the label.

  • Lifetime Qt Champion

    It would if you had scaledContents set to true. Have a look again at the example description just above the adjustScrollBar code block

  • Hi,

    I´ve already tried with scaleContents both true and false, both from Designer and hard coded, and still it won´t work.

    Still can´t understand why. I think is a layout problem but don´t know how to fix it yet.

  • Lifetime Qt Champion

    I can't really comment since I can see any layout from your images.

    However, since there's not much code involved. Copy the code from the example and run it. Once you are sure it works properly, modify it to suite your need. That might be quicker to get to your goal.

  • Hi,

    Yes I might try that. The idea was to use Designer to do things faster, but I guess I´ll have to write the code for the GUI by hand.


Log in to reply