2

I'm currently building an application that consists of a QStackedWidget with several pages. The page itself was added in the designer. What I CAN do (in code):

  • add a QVBoxLayout to the page
  • add some custom widgets to it
  • set layout for the page

This works quite well, I can see my widgets appear on the page. These widgets have a fixed height of 25. When there are too many widgets, I can't see all of them. What DOES NOT work is adding a QScrollArea to the page that allows scrolling up and down, in case there are lots of widgets added to the page.

So here is my code for the situation "as is":

//The header file:

QVBoxLayout *valuesLayout;

//The corresponding .cpp file

valuesLayout = new QVBoxLayout();
valuesPage->setLayout(valuesLayout);  //valuesPage is my QStackedWidget page
for (int j=0; j<100; j++)
{
    valuesLayout->addWidget(new PaIndicator(0, "This is a test", 0));  // my custom widgets
}

How would I have to change/extend the code from above to make my widgets appear in a QScrollArea?

Update: After applying the changes mentioned below I ended up with this: enter image description here

My now code looks exactly like the lines given in Shf's answer. I have the feeling that I'm getting closer, but something still seems to be wrong here.

Rob
  • 11,492
  • 14
  • 59
  • 94

2 Answers2

4

You have to set a widget to your scroll area and add the contents to that widget:

QScrollArea *scrollArea = new QScrollArea;
QWidget *scrollWidget = new QWidget;
scrollWidget->setLayout(new QVBoxLayout);
scrollArea->setWidget(scrollWidget);
for (int j=0; j<100; j++)
{
   scrollWidget->layout()->addWidget(new PaIndicator(0, "This is a test", 0));  // my custom widgets
}

Then add your scroll area to your stacked widget's page:

valuesLayout->addWidget(scrollArea);
thuga
  • 12,601
  • 42
  • 52
  • That did it. One thing that was important for me: using the above solution does not expand the added (inner) widgets like they would normally do when being added to a layout (i.e. fixed height, expand horizontally). To enable this behaviour you have to set this: scrollArea->setWidgetResizable(true); – Rob Jan 21 '14 at 12:01
  • 1
    @Robert: What's missing is a call to `layout()->setSizeConstraint(QLayout::SetMinAndMaxSize)`. One doesn't have to call `scrollArea->setWidgetResizable`, then. The former possibly expresses what's intended in a cleaner way: the widget has to be as large as needed to fit the contents. – Kuba hasn't forgotten Monica Jan 21 '14 at 20:04
2

You would need to add to valuesLayout only QScrollArea object, let's say scrollArea. You would need to create QWidget, which would be in scrollArea, let's say scrollWidget and set QVBoxLayout for scrollWidget, let's say scrollLayout, now you can add your widgets to scrollLayout and they would appear inside your QScrollArea, so the code should look something like this (it's a bit tricky and cofusing, but will be easy in time):

//The header file:

QVBoxLayout *valuesLayout;
QVBoxLayout *scrollLayout;
QScrollArea *scrollArea;
QWidget *scrollWidget;

//The corresponding .cpp file
    valuesLayout = new QVBoxLayout(); // creating layout for valuesPage
    scrollArea=new QScrollArea(valuesPage); // creates scrollarea, and set valuesPage as it's parent
    scrollWidget =new QWidget(scrollArea);  // creates scrollwidget, your scrollArea as parent
    scrollLayout = new QVBoxLayout(scrollWidget); // creating layout in scrollWidget
    for (int j=0; j<100; j++)
    {
        scrollLayout->addWidget(new PaIndicator(0, "This is a test", 0));  // adding your widgets to scrolllayout
    }
    scrollArea->setWidget(scrollWidget);  // sets scrollArea around scrollWidget
    valuesPage->setLayout(valuesLayout);  //valuesPage is my QStackedWidget page
    valuesLayout->addWidget(scrollArea ); // adding scrollwidget to your mainlayout
Shf
  • 3,463
  • 2
  • 26
  • 42
  • Thanks for your answer, I understand the structure now. Unfortunately something still does not work as expected as there is no scrollbar and things look somewhat squeezed. I updated my first post and added an image. – Rob Jan 21 '14 at 07:54
  • 1
    @Robert you are correct, there was several mistakes, fixed them and updated comments, forgot key part - `QScrollArea::setWidget()` – Shf Jan 21 '14 at 11:41