Creating a Form Layout
This one is slightly different compared to what we have seen before. Now for the first step, you still have to create a layout instance, and this one works in basically the same way compared to what we have seen earlier. But the next step is for this layout, you don’t add another widget. Instead, you would add another row, so you would use the method
.addRow(). And into this, you have to add two arguments. The first argument is for the column on the left, the second argument is for the column on the right. So for example, for the column on the left you could have one argument that just specifies
'name', and then put a column on the right, so the second argument, you would have a
QLineEdit widget—something we are going to implement in just a second, actually.
00:49 And for the entry on the right, you always need a widget, but for the entry on the left, it could either be a widget or you could just add a string. But let’s implement all of this—I think that is going to explain all of it the best.
So here you can see the basic code we have used so far, and this one should still look fairly familiar although now there are a couple more changes. Number one is we are importing a
01:23 So now we don’t use a button anymore—instead we use text and line edits. So widgets that are more concerned with text than with just pressing a button. But they work in exactly the same way as far as the layout manager is concerned.
This one is going to give us the instance that we are going to use in just a bit. And then a bit further down, we are setting this
layout to be the layout of our main widget—so this one here. And now all we have to do is to take this layout and
And in here, we have to add two arguments. The first one is the widget or the text on the left column. And let’s pick a simple one. I just want to go with
'Name', then a colon and a space just to give it a little bit of space. And for the second column, I want to add a widget, which in my case is going to be the
So, My name. And now for any additional row, we could just add more information. So for example, I could duplicate this entire line, then I have another row, and let’s call this one
'Job: '. And I still want to keep my
QLineEdit(), so this one stays exactly the same. And now if I run all of this, we get two lines! One with Name and one with Job, so this one is working pretty well. Nice.
Now, you can make this a little bit more extensive. The text on the left could also be a widget, and let me demonstrate this. Let’s say, for example, I want to add another row that asks for the email address, and the text for email shouldn’t just be a string. Instead, it should be a
QLabel, so the one we imported early on. So let’s first create a
So, this one is working reasonably well! And the left side here doesn’t necessarily have to be text. It could effectively be any kind of widget. For example, I could copy the
QLineEdit(), and paste it in here, and now if I run the code, now both on the left and on the right side we have a
Although, granted, this doesn’t make too much sense, but maybe in a specific case this would be useful. And with that, we have learned about the fourth layout manager, the
QFormLayout, where we just have two columns. That makes it very easy to create a very basic form or to arrange generally anything in two columns. Now, in the next lesson, we are going to learn how to customize the four layout managers in a bit more detail. I will see you then!
Become a Member to join the conversation.