Creating a Vertical Layout
The only real difference is that now we use a different layout manager. This one is called
QVBoxLayout. All it really does is it takes a couple of widgets and then places them in a vertical layout.
Really all we have to do is do the same thing we have done in the last video and just change one letter from an
H to a
V. Let’s jump into our code and let’s have a look at this! Here again, I have the very same setup that we have seen in the last video.
And the other difference is that the title of our GUI now is
"QVBoxLayout Example". Besides that, it is the exact same code. So now again, if I run it, we get an empty window that we can’t really do anything with. So now again, what I want to do is—let me add a comment—to create a
And what is going to be happening in here is that each widget is going to be added from the top, so the first widget is going to be on top, the second widget comes below that, and any additional widget will always be added below that. And with that covered, all we have to do—let me add another comment—is to set the layout to the application’s window. And this, again, happens with
02:33 And then here, we need one argument that is our layout. And this is all we needed! So if I run the code now, we can see that we have three buttons that are working in the vertical way. And even if I scale the widget, it still works really well.
02:49 And obviously, we can also still click on the buttons. This works in exactly the same way. So this would give us a very basic vertical layout. Now, there’s one more thing we could be doing here, and let me add this a little bit further to the top. So if I run this code, our starting window is really small.
And I want this to be a little bit larger, let’s say roughly something like this, so that you can read the title of the window and you can see the buttons a little bit better. To achieve that, all we need is to target our parent widget and type
So I want my window to be 270 pixels wide and 110 pixels high. So now if I run this, this is a little bit larger. Still not very large—you could, for example, add something like
570 in here, and now this would be much wider. But let’s stick to
Become a Member to join the conversation.