I am working on a QDialog configuration. It has several categories (general, appearance, etc.) that are loaded when the user clicks on them. Each category has its own page. These pages are separate classes in their own right (each of them has its own ui, cpp and h). When the settings window loads, the pages become instances and load into the QStackedWidget. Then the Stackedwidget is placed in the QScrollArea, so it can scroll if necessary.
However, there is a problem. Since I added QStackedWidget, QScrollArea always has a vertical scroll bar, even if the current page is short enough so that it does not need it: (image shows the shortest page)

ScrollArea's vertical scroll policy is set to Qt :: ScrollBarAsNeeded, so logically it should only show the panel if the page is larger than the viewport.
Here is what I have already tried to fix:
Setting the scroll policy in Qt :: ScrollBarAlwaysOff. Although this eliminates the scrollbar, it is unacceptable because it does not allow the user to know that they need to scroll through long pages.
Setting minimum / maximum height for QStackedwidget. This causes the scrollbar to go away if I set it to a low enough value, but this is unacceptable, as this causes some of the widgets to have a hunched look.
I know the problem has something to do with QStackedWidget, but since this is the first time I have used QStackedWidget, I am not sure what it is. I also noticed that the scroll is always the same amount; that is, the scrollable area always has the same size, regardless of how large / small the page widget is. For some reason, it is slightly larger than the longest page. At first I thought that the vertical spacers that I placed at the bottom of each page to tighten the layout caused this, but it didnโt fix them.
Update: here is the code that controls the Stackedwidget:
void Newconfig::on_Categories_currentItemChanged(QTreeWidgetItem *current) { QModelIndex index=ui->Categories->currentIndex(); int idx=index.row(); QString category=current->text(0); this->setWindowTitle("Preferences -- " + category); if (stack->currentWidget() != 0) { stack->currentWidget()->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); } stack->setCurrentIndex(idx); stack->currentWidget()->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); adjustSize(); }
source share