Qt World Summit: Register Today!

How to create a Grid Layout, containing images and labels, as Widget Layout of Vertical Scrollable Area

  • Hi everyone.

    I'm new of Qt and I just started to make the GUI of my project but I'm stuck on the Scroll Area part.

    Since the concept of what I'd like to do is a bit complicated I did sketched it with paint (with my awesome paint skills!!):


    To achieve this result I managed to create the Scroll Area in this way:


    QScrollArea*SA = new QScrollArea(this);
    SA ->setGeometry(100,64,352,250);
    SA ->verticalScrollBar()->setStyleSheet("QScrollBar {height:0px;}");
    QVBoxLayout* VBL = new QVBoxLayout;
    VBL ->setMargin(0);
    VBL ->setSpacing(0);
    QWidget* containerWidget = new QWidget;
    SA ->setWidget(containerWidget);


    I'm stuck at this point. At the moment some QPushButtons were added (VBL->setWidget(QPushButton);) to test if everything was working fine and it does, but the widget I'd like to add is the whole Object of the Scrollable Area shown in the picture.

    Does anyone have any idea about how to create this?

    Thank you in advance!

  • Lifetime Qt Champion

    @cppStudent012 I'm not sure what the problem is. For each of these objects inside the scrollable area you can simply insert a widget (and add this widget to the layout) and on this widget you put all the elements (don't forget to add a layout to this widget).

                    Q*Layout (check which layout is the proper one for your use case):

  • @jsulm Thank you for answering!

    I didn't understand that every single Layout object had to be wrapped in a QWidget one.

    Everything works fine in the way you said!

    Thank you very much!

  • @jsulm My case is similar, but my question is what parts (wigiets and/or layouts) get repeated, for several objects of the scrollable area?

  • Lifetime Qt Champion

    @harveyab It depends on what you want to do. Since I don't know what you want to do I can't tell you. In general: widgets can contain other widgets and layouts same goes for layouts, so you can do what ever you want.

  • @jsulm Thanks for your response. So in his example as given in his drawing, what part of your answer gets repeated for the items that are "listed" in the scrolling area? I understand that the classes are not repeating, but I mean there are multiple instances of some classes. At what point do the instances have multiples? Is it the QVBoxLayout, the widget above it, or does it start with the widget below it? I have other questions, but I need to start somewhere. In my case I will have one picture file open, but in each listed item, I will have a part of that one picture showing, and some editable text with each. In what class do I open the image file? Do I 'get' the image part from below, or do I 'set' the image part from above? Does that make sense?

  • Lifetime Qt Champion

    @harveyab Everything bellow QScrollarea is repeated as often as needed.
    It really depends on your use case. If you want to have exact answer then you have to ask a precise question.

Log in to reply