Important: Please read the Qt Code of Conduct -

QScrollArea Image Loading

  • Hello, everybody,

    I am trying to modify the "scribble" Qt example and load an image inside a QWidget and add scrollbars if the image is larger than the Main Window.

    I have these two classes:

    class MainWindow: public QMainWindow
    class ImageViewer: public QWidget

    Inside the constructor of the MainWindow, I have added the following code.

    QScrollArea *scroll_area_ = new QScrollArea();
    main_area_ = new ImageViewer(scroll_area_);
    QHBoxLayout *mainLayout = new QHBoxLayout();
    QTextEdit *te = new QTextEdit("1234"); // Added also a text box just for testing
    setWindowTitle(tr("Show Image"));

    and in my main I have added the following code:

    int main(int argc, char *argv[])
        QApplication app(argc, argv);
        MainWindow window;;
        return app.exec();

    My problem is that the widgets are being loaded one behind the other and when I am loading a large image file the scrollbars do not appear.

    Do you have any ideas what I am doing wrong.?

    Thank you in advance for your help.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are setting a layout and a widget on your scroll_area which is wrong. From your description, you should rather have your ImageViewer as widget of the QScrollArea and then the QScrollArea in a layout on top your QTextEdit.

Log in to reply