I would need someone to teach me some things about QScrollArea.
-
Hello, please just as we have the .setWidget() method when using a QScrollArea, do we have the .setLayout() method for it? Please pardon the structure of my sentences.
I mean this for example:
<>Class app(QWidget): (the function def init and super codes) grid = QGridLayout () self.setLayout(grid) vbox = QVBoxLayout() gbox = QGroupBox() grid.addWidget(gbox) gbox.setLayout(vbox) hbox = QVBoxLayout () vbox.addLayout(hbox) btn = QPushButton() hbox.addWidget(btn) scroll = QScrollArea () scroll.setWidget(gbox) scroll.setVerticalScrollBarPolicy(Qt.ScrollBarwaysOn) scroll.setHor....(..AlwaysOn) grid.addWidget(scrol)
</>
I wrote these codes off heart just to explain my point so I can be educated properly. From the codes, it can be seen that grid is the parent layout, and its layout is set on the QWidget subclass, window or self.
Now, I set the gbox which is a group box on the scroll area. With the little I've studied from practices, anything coded within the gbox will be scrollable based on the code here, and anything outside the gbox won't be scrollable.
Please is there a way I can make the whole contents of the Window scrollable? Can one use scroll.setLayout() on Qt instead of scroll.set() so as to make the whole components resting on that layout scrollable?
Please, I am not an expert but an enthusiast who loves practicing, taking note of observations and then asking questions when necessary so I can be better enlightened and informed. Kindly enlighten me and help me
-
Usually you don't set a
QLayout
to aQScrollArea
since it kinda does the opposite thing.
Btw: I hadn't realized that your other topic contains a question (i.e. this question, about how to use scrollArea). I thought it was a question to those, who read your "guide".Create your widget, apply a layout with the content and add this widget as such to the scrollArea with
addWidget
.
TheQScrollArea
would be yourcentralWidget
then (assuming you are using aQMainWindow
). Keep in mind that the content of aQMainWindow
cant take the whole window space. Some space at the top and bottom is reserved for toolBars, statusBars and other stuff.If I'm not wrong
setLayout
is only available because aQScrollArea
is a widget itself, so it inherits this function fromQWidget
base class.
Like almost everywhere, the same "rules" apply in programming: Just because you can do something, it doesn't mean that it's recommended to do or that you should do it :)Edit:
Your question was already answered by @SGaist and @JoeCFD here:
-
@Pl45m4 smiles.
I believe I will be learning a lot from you because you seem more versatile and I notice you're always trying to help coders facing challenges resolve their issues. Thanks for this.I actually tried the scroll.setLayout(), no error in the code but the scrollArea became inactive. From your explanation and what I studied from RealPython.com site, it therefore means the QScrollArea is not a subclass of QWidget but a widget.
Now, is there any way I can make the whole contents of a Window scrollable using the QWidgets class? I don't want to use QMainWindow class, I know I can use the .setCentralWidget() method there.
How can I make a Window with different layouts of same level scrollable?
Is it that there must be a parent layout?
-
Hi,
@CEO said in I would need someone to teach me some things about QScrollArea.:
I actually tried the scroll.setLayout(), no error in the code but the scrollArea became inactive. From your explanation and what I studied from RealPython.com site, it therefore means the QScrollArea is not a subclass of QWidget but a widget.
You are wrong there, see the QScrollArea documentation. You'll see it inherits QAbstractScrollArea which inherits QFrame which inherits QWidget.
-
I gave you the full chain of inheritance back to QWidget.
Yes you can put a layout on an QScrollArea. As already said by @Pl45m4, it's not what you do when you want to use QScrollArea to manage a widget bigger than for example the screen. Still, there might be cases where putting something in a layout on a QScrollArea could make sense. However I do not have an example at hand.