Having challenges in understanding QScrollArea with QLayouts in PyQt5? If yes, then read my post below.
-
Hello great minds,
I learned and understood some things about QScrollArea and other QLayouts today and I will share. Please be free to counter me where you think I'm wrong or where you think I should be more enlightened.
Adding QScrollArea to Windows usually gives a tough time to codes enthusiasts/programmers especially those like me without physical tutors.
Now, when dealing with graphical user interface (gui), before writing our codes on any platform, we have what we call Window. This Window is the graphical interface which displays the content of our our codes when executed.
Now let me go to QLayouts.
As we progress in building GUIs, we would need containers to arrange our codes and these containers can have smaller containers in them. I will call the biggest of them all an ancestor.
This ancestor container can be a gridLayout (gbox) a verticalboxLayout (vbox), a horizontalboxLayout (hbox)
I will be using gridLayout (grid) as the ancestor layout here for an example. You can use a hbox or a vbox for yours.
Now take note of these vital information.
You create a platform on which your ancestor layout would rest on, it's just like a house which needs a foundation to rest on.
Now, an ancestor layout can only rest on a subclass of QWidgets including Window or Form
gridbox = QGridLayout()
self.setLayout(grid)The self keyword there represents Window automatically.
Remember, after instantiating your code for the ancestor, write the code specifying the platform on which you want your layout to rest on, and in this example, it is Window represented by self. You can instantiate form and set your ancestor container to it.
You can add other layouts to your ancestor container.
You can set a qGroupBox to your ancestor container.Whatever you want, always write the code immediately after you've written your ancestor container.
For instance:
grid = QGridLayout()
self.setLayout(grid)gbox = QGroupBox()
grid.addWidget(gbox)Note: You can not use the .addWidget() method for a QGroupBox. I mean you can't write gbox.addWidget(). You also can't write gbox.setLayout().
You can only use it as a platform on which other layouts depend or rest on.I am instantiating a new Layout
vbox = QVBoxLayout ()
gbox.setLayout(vbox)You can add a gridLayout, a verticalboxLayout and a horizontalboxLayout on each other but can't add them on a groupbox but can use a groupbox as their base.
(Please I'm attending an online class now, stay tuned...)
Back…
As you can see, the vbox layout is set on a groupBox (gbox). A GroupBox (gbox) will not display on our window unless it has a layout resting on it, in this case, the vboxLayout. If after writing your call and calling the groupbox widget but fail to set a layout on it, the groupbox will not be displayed.Without a layout set on the gbox, when the code is run, the gbox won't display anything.
The layout you add to the gbox is where you would then add the children widgets.
Then you instantiate your QScrollbar area, one of your QWidget classes should be set on this area. Then you add the scrollarea as a widget to your ancestor container or any of the layouts of your choice housing the other layouts.
Note:
All these must be coded in an order of precedence else you would either not get your expected result or your code would output error.If you write the QScrollArea codes before any of the others, you won't get the expected result.
scroll = QScrollArea()
scroll.setWidget(gbox)
scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
grid.addWidget(scroll)As you can see, the scrollarea makes use of the gbox and as well as the grid, so you must have coded everything concerning the gbox and grid before you instantiate the QScrollArea to avoid error.
In those QLayouts used (hbox, vbox, grid), you can choose to add other Layouts on each of them, and add widgets to each of the layouts until you achieve your desired result.
I would like to know what happens when one sets a Layout to a scrollarea. Any idea please?
Again, if anyone has any question, be free to ask.
-
All you've said above is pretty well explained in the Qt documentation :)
- https://doc.qt.io/qtforpython/overviews/layout.html#layout-management
- https://doc.qt.io/qtforpython/PySide6/QtWidgets/QScrollArea.html?highlight=scrollarea#qscrollarea
I don't want to roast you, but there are some statements that are wrong, not completely correct or situations described with wrong terms :)
When you are a beginner as well, you shouldn't write guides or tutorials... Others might get confused.Now, when dealing with graphical user interface (gui), before writing our codes on any platform, we have what we call Window. This Window is the graphical interface which displays the content of our our codes when executed.
This is not like the big bang or the Genesis where there suddenly is a
(Q)Window
... :)
Without code, there is nothing :)
If you create a new project, there is probably a default window created, but it's because of the settings ofQtCreator
andQt
itself.As we progress in building GUIs, we would need containers to arrange our codes and these containers can have smaller containers in them
I wouldn't call it "containers". A layout is a layout. With layouts you can manage the behavior of your widgets / content while resizing and, well, lay-out your stuff including size hints, policies etc.
A container forints
could be aQVector<int>
for example. Or some widget that contains other widgets to manage them. This widget can/should also have a layout to define how the content behaves.Adding QScrollArea to Windows usually gives a tough time to codes enthusiasts/programmers especially those like me without physical tutors.
?
It's not that complicated as it might look.Without a layout set on the gbox, when the code is run, the gbox won't display anything.
Kinda wrong. You can display everything without having a single layout, but then it's one single mess.
If you add everything to a layout and don't apply your layout to your container widget, THEN you don't see anything :)Note:
All these must be coded in an order of precedence else you would either not get your expected result or your code would output error.Not 100% correct. You can create all objects one after another and then start working with them. Of course you need to create a widget first, before you can add some content.
As you can see, the scrollarea makes use of the gbox and as well as the grid, so you must have coded everything concerning the gbox and grid before you instantiate the QScrollArea to avoid error.
This is the opposite of what you did. As I've said above, you can create an empty scrollArea, then create the content and add it to the area. Or you create the content, then the scrollArea and then add it... makes no difference.
-
thanks for your contribution but you could have been a bit mild, not sounding like a condemnation instead of a polite criticism.
Now point of correction, this is a post written from observation based on my recent practices. I decided to put this post here because I had a tough time understanding QScrollArea. I teach myself, browse the net for information but often times, most of what I got are just too difficult to grasp because they're not elaborative.
I don't think you've written any post like this aside from writing few lines of code to someone without even minding if the person is a beginner or not.
And as for the container aspect, if you had read the post with an open mind, you would have understood where I said "..as we progress in building GUIs". This would or should have informed you that probably to get to where I aim to focus on, one must have gained knowledge of building GUIs primarily without using layouts.
And also, I stated it on my post that layouts can be added on a layout (nesting). I did not force you to call it a container. You sounded more like a hater, there are better ways of criticizing a write up but yours is like one borne out of sheer hypocrisy, no apology.
The best way to teach a child is to bring oneself to the child's level. If I can not illustrate what I teach in different ways, using different examples to analyze, then sorry, I wouldn't be a good teacher. A teacher must be able to use different illustrations to pass a message. I said a layout is like a container and I stand by it because it manages and houses widgets.
Again, if you understood perfectly well everything on Qt documentation, everyone must not understand everything there. I did not understand and that's why when I understood, I decided to share my discovery so others who are having challenges like I had could have other sources to learn from.
While in school, it was not and is not advised to study from one textbook. Studying from different textbooks exposes us to different ideas, opinions and explanation of a particular topic and this broadens our understanding of the topic. Having gained a broad idea, one can explain it to others using different examples.
Gentleman, next time, it will be ideal you learn to criticize others to learn well and improve on oneself, not to condemn others, their effort, make them look stupid and even contemplate giving up.
If you learned nothing from this post, leave it, others will learn and I know I will also fall back on it some time later if I experience any challenge.
A layout can be seen as a container.
I talked about an order of precedence. If you are versatile, you would have coded what I meant by that. You are free to initialize your container codes after you've initialized your QScrollArea, then work with your errors.I doubt you've ever written a piece like this off heart.
-
@CEO said in Having challenges in understanding QScrollArea with QLayouts in PyQt5? If yes, then read my post below.:
thanks for your contribution but you could have been a bit mild, not sounding like a condemnation instead of a polite criticism.
That was not meant to sound too hard.
Programming is not that simple. To understand better what is going on, not only writing code as such, but the whole software developement process including what will happen if you click build / run on your program, it's very important to be precise and use the right terminology. Otherwise people would get even more confused.
If you just managed to understand how some things work, how can you be a good teacher then?
I'm not a beginner anymore, but I'm far from being an expert. And sometimes it's gets really complicated when trying to explain something to a total newbie.make them look stupid and even contemplate giving up.
I never said you should give up.
The title of your topic suggests that the following text contains the best and clearest explanations aboutQScrollArea
,QLayouts
and other stuff. Which, I feel, is not accurate.It feels like it is directed to absolute beginners in programming (nothing wrong with it), but Qt is a GUI framework (for Python, C++...) which requires some basic knowledge before you jump right into it and dont quite understand what's going on.
I dont know if it's just me, but I cant be the only one who feels like more confused after reading this (even tho I knew all this before), because other tutorials probably use other terminoloy. In my eyes, the official Qt documention and the Qt source code, is the best place to look at, when having issues to understand something.
But if this will help somebody in the future, it was worth it :)