Qt Creator's Designer generates hidden QWidget for layout, and this behavior results in a QScrollArea problem
-
I observed that Qt Designer generates a hidden QWidget whenever a QLayout is added to a QWidget, and sets that hidden QWidget between the seemingly parent QWidget and the QLayout.
And if I add a QLayout to a QScrollArea, then the object tree in the Designer looks like this:
As shown in the image, a QVBoxLayout added to a QScrollArea's QWidget. I cannot add layout to scroll area directly, just to scroll area's widget, and no matters if I set scroll area as ancestor or not, the result is the same. Moreover, the widget of the scroll area cannot be deleted.
The code snippet below (from ui_widget.h) shows the generated hidden QWidget (for the object tree shown above):scrollArea = new QScrollArea(horizontalLayoutWidget); scrollArea->setObjectName("scrollArea"); scrollArea->setWidgetResizable(true); scrollAreaWidgetContents = new QWidget(); scrollAreaWidgetContents->setObjectName("scrollAreaWidgetContents"); scrollAreaWidgetContents->setGeometry(QRect(0, 0, 394, 597)); verticalLayoutWidget = new QWidget(scrollAreaWidgetContents); verticalLayoutWidget->setObjectName("verticalLayoutWidget"); verticalLayoutWidget->setGeometry(QRect(110, 150, 160, 80)); verticalLayout = new QVBoxLayout(verticalLayoutWidget); verticalLayout->setObjectName("verticalLayout"); verticalLayout->setContentsMargins(0, 0, 0, 0); scrollArea->setWidget(scrollAreaWidgetContents);
As can be see, the hidden QWidget is that whose name is "verticalLayoutWidget", it is hidden because it isn't showed in the Designer's object tree.
So instead of setting the "verticalLayout" directly to the "scrollAreaWidgetContents", the generated code set it to the "verticalLayoutWidget".
And the problem with this solution is that the scroll area won't be scrollable if there are lots of widgets in it. I tested that if I connect the "verticalLayout" to the "scrollAreaWidgetContents" directly in the generated code (by modifying the generated code through code editor), then the scroll area will be scrollable.So I have no idea, how can a layout in a scroll area be scrollable with Qt Designer.
-
Sorry, I forgot to mention the used versions:
Qt Creator version: 11.0.3
Qt version: 6.6Hi @fokhagyma
You must set the scrollAreaWidgetContents layout with right click > layout > Vertical layout
You can do that by clicking in the scrollArea in the designer,
or by clicking in scrollArea in the tree object view panel.No need to create a second widget.
-
Sorry, I forgot to mention the used versions:
Qt Creator version: 11.0.3
Qt version: 6.6Hi @fokhagyma
You must set the scrollAreaWidgetContents layout with right click > layout > Vertical layout
You can do that by clicking in the scrollArea in the designer,
or by clicking in scrollArea in the tree object view panel.No need to create a second widget.
-
Hi @fokhagyma
You must set the scrollAreaWidgetContents layout with right click > layout > Vertical layout
You can do that by clicking in the scrollArea in the designer,
or by clicking in scrollArea in the tree object view panel.No need to create a second widget.
-
-
@mpergand Thank you very much.
Yes, it works but only after I drag a widget into the scroll area (at empty scroll area the layout options are greyed out).
@fokhagyma
QDesigner is not the best/fancy app in the world.