QScrollArea sizing issue
-
No, the content of the widget is described correctly on that post.
The two points of interest:
- on the bottom is parent widget (for example QDialog)
- on top of this is scrollable area (QScrollArea) of fixed size
You see ? No layouts involved there. The fact that it's passing a parent to the QScrollArea makes it appear within the dialog.
-
@SGaist
Thanks, but I thought I was to do the same as him, yet in mine you're telling me I need aQVBoxLayout
that he does not need.....I started with a dialog with a vboxlayout and some content. My content overflows the dialog vertically. I want to have a vertical scrollbar against the side of the dialog for its content. That's all I want to add to the original code. I'm finding this mega-complex.... :(
-
What the examples shows is how a QScrollArea work not how to make it fit in a widget.
From your description you want your scroll area to fill your widget so that you don't have to resize it, hence you need to put the area in a layout applied to said widget.
-
@SGaist
OK, thanks, I'll have a play.All I need to understand is the hierarchy I need. If I started with
dialog -> vboxlayout1 -> content
I will now need
dialog -> vboxlayout2 -> scrollarea ( -> widget ?) -> vboxlayout1 -> content
Is that right? I'll need a separate vboxlayout from the one I started from, I don't somehow apply the scrollarea to the original vboxlayout? -
To make things clearer:
widget -> vboxlayout1 -> content
<- future content of the QScrollAreadialog -> vboxlayout2 -> scrollarea
<- QScrollArea fitting the "main widget"scrollarea -> widget
<- finally your widget in the QScrollArea -
@SGaist
OK, armed with the understanding of what the actual hierarchy should be, I have now modified to outline:# self == QDialog self.dialogScrollPanel = QVBoxLayout() self.setLayout(self.dialogScrollPanel) scrollArea = QScrollArea(self) scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) scrollArea.setWidgetResizable(True) self.dialogScrollPanel.addWidget(scrollArea) widget = QWidget() scrollArea.setWidget(widget) self.vertLayout = QVBoxLayout() widget.setLayout(self.vertLayout) ...
And sure enough my dialog now looks like:
which is just what I was aiming for!The one bit I'm left hazy on is when you do or do not need to specify an owner/parent for a layout or widget. My code includes:
self.dialogScrollPanel = QVBoxLayout() self.setLayout(self.dialogScrollPanel) widget = QWidget() scrollArea.setWidget(widget) self.vertLayout = QVBoxLayout() widget.setLayout(self.vertLayout)
In each of these cases the constructor could equally be
QVBoxLayout(self)
orQWidget(self)
and I see the same behaviour. In the code I've inherited it seems sometimes it passes an argument to these and sometimes not. When do I need or not need to pass a widget parent/owner argument when creating a widget or layout? -
@JNBarchan
Any widget that is not given a parent will become a window.
So for any widget on a from, you will want to give it a parent.
However, inserting them into other widgets set the parent.For Layouts, its its easier just to assign them to a widget and in that
way give a parent.So in most cases you will just want to give a parent when constructed as
to make sure they are deleted. -
I usually follow these rules:
- When I create layout that should be the "main" layout of a widget, I pass that widget as parent
Note that's just an habit, if you prefer the setLayout method then go for it, just be consistent all over your code base. - All widgets that are put inside a layout will be re-parented appropriately so I don't pass a parent to them.
- Unless I'm going to manipulate the layout later on I don't make it a member of the class
- When I create layout that should be the "main" layout of a widget, I pass that widget as parent