Creating Two Forms for the Graphical User Interface
Form 1 - Main Window
Step 1 - Creating the Main Layout (6:31)
Step 2 - Adding the Widgets of the Query Term Section (4:48)
Step 3 - Adding the Widgets of the Service Section (12:24)
Step 4 - Adding the Widgets of the Results Section (3:36)
Step 5 - Adding the Widgets of the Add Features Section (11:01)
Jan Oliver Wallgrün: The fourth row of our layout is, again, very similar to the second one. You have the tab widgets, with the button below it, and then you have to set up the widgets for the different tabs.
So first thing is to take a tab widget and their containers and drag it over. Drop it onto the group box for add features. And then, I'm going to change the layout out of the group box to use a vertical layout. And I'm going to add a third page to that tab by doing a right click on top of it and using insert page.
And now I'm going to give the tabs the right names in the property editor. Again, we have to scroll down to the green area. And there, on the current tab text, you change the titles. The first one should be called Shapefile. The second one should be called Layer. And the third one should be called CSV.
And you have to make sure that you rename the entire tab widget, as shown in the graphic that's in the figure in the lesson materials. And also the individual tabs-- so, these entries here in the hierarchy-- need to be renamed.
And then we can add the horizontal layout for the single button here. So that's something you have seen before now. I'm going to drag the horizontal layout, drop it below the tab widget. And then I'm going to add a push button to that horizontal layout, and a horizontal spacer, so that the button is pushed all the way to the left. And the title of the button-- double clicking it again and changing it to Add.
OK, at this point, we just have to create the content for the different tabs, starting with the Shapefile. Like the tabs above for the services, all of these are using grid layouts to arrange the items, starting with a button that is for creating a new Shapefile. So I'm dropping a push button into the Shapefile tab widget. And then I'm going to do a right click and change the layout to grid. And the title of the button should be changed to Create new shapefile and three dots, because this one is going to open a dialogue, which is indicated by the three dots.
Then we have two labels below it. I'm going to add the first label, and changing it to say Add to shapefile:. And then the second label, you know it, this one says Field to store name:.
And then for the second row, we need the line editor widget for entering the name of a Shapefile. I'm dropping it here with the vertical blue bar on the right. For the third row, we need a combo box widget. It's one which allows users to pick between a small number of choices. I'm going to place there. It will be a horizontal layout.
And then we're going to add the combo box to that horizontal layout. And we add the horizontal spacer. So the effect of that is that, just, the combo box, again, is not expanded all the way, but just as much as needed to display the items in it.
And then the last thing missing is this tool button we have here that is going to open with file dialog for picking a Shapefile. So we take a tool button under buttons, and dropping it in the second row to the right of the line edit widget, like this. And then we have set up everything for this Shapefile tab, and we can move on to the Layer tab.
Layer looks somewhat similar. We can start by adding the two labels. One will be called Pick layer:, and the second one will be called Field to store name:. And we're setting the layout of this tab to use a grid.
And then we actually have two combo boxes for both rows. So I'm dragging a combo box and place it to the right of the Pick layer label, and another combo box to the right of the Field to store name label. And then we have another tool button for refreshing the list of layers. So the tool button, I will drag over and drop to the right of the first combo box.
And finally, so that all of this does not take more space than needed, we're going to place a horizontal spacer as a kind of false column. And then everything will be pushed over to the left as much as possible. So that's it for the layout tab. And we can move on to the CSV tab.
And that one is really simple now. It just has to create this single row. So you could also use a horizontal layout for that. So I'm going to start with the label, call that label Add to file:. Now I'm going to do right click layout, lay out horizontally. And then I'm going to drop the line edit for entering the name of the CSV file to the right of the label, and finally, another tool button for opening a file dialog, to the right of the line editor widget, like this.
And I'm saving everything. So basically, we have the view for our main window done. There's just a little thing to do this exit tool button we have at the top. For this one, we have to set up an action so that we can directly say, inside Qt Designer, that this tool button here at the top should, basically, close the application.
The action editor can be found here at the bottom right. And I just click this button, the first button at the top, which is for creating a new action. And then I'm filling out the dialog. The text for that action should be Exit. The Object name, actionExit-- that can remain like it is. And well, we can enter a ToolTip, Exit program, for it.
And that's basically all we're going to do here. You're going to pick an icon for this later on from our code, using the standard set of icons coming with Qt. So I'm pressing OK, and now we have this action in our list of actions here. And I can actually take the action and drag it and drop it on top of our tool button here, on top of the main window. And we will now see that we have the action showing up there as an exit. And we're going to replace that text later with the icon, as I said.
So far, we have not said what the action should do. So I'm going to change to the signal and slot editor now, by clicking on the tab below the action editor below. And I'm now going to add an entry there by clicking on the plus symbol. And for center, I'm picking the action exit that we just created.
Then I'm picking the signal triggered, so meaning whenever action exit is triggered-- for instance, by clicking on the exit button of the toolbar-- the signal should be sent to-- and now, I'm picking our application. So I'm clicking the main window, and I'm picking the close slot, so meaning, when the signal is sent to the main window, its close method will be executed and the application will be closed. Now I'm saving the program.
And let's look at the preview for a moment. So we see the elements are all there, the different tabs are showing up correctly. But obviously, we don't have much functionality. So not a lot is going to happen, except that we can use, already, the tool button to close the application.
Form 2 - Creating the New Shapefile Dialog (3:37)
Jan Oliver Wallgrün: I said at the beginning that we will need to create two forms, two graphical user interfaces. We have now finished the one for the main window, but we still need to create the form for opening the Create New Shape for dialog. So if I click on this dialog actual tool, on this button, I get the dialog box where I'm allowed to enter the name of the new shapefile and the field that should be added to it.
So plus, we still need to create, so I'm going to use File, New, and now, I'm going to pick Dialog With Buttons Bottom as the template to use and then press Create. And we now get a new form, which already has two buttons created, and you have to set it up that it looks like the dialog box we want.
First of all, let's change the title. So under Properties, we go to Window Title, and then we call this Dialog Create Shapefile, and we save the whole form under the name gui_newshapefile.ui.
OK. Now we have things set up. We can now dot at the missing widgets, and for that, I'm going to drop a grid layout at the top of this, and then I'm going to change the layout of the entire dialog box to a vertical layout. As a result, I still have the buttons, the default buttons that they are already created by QT designer at the top right, and we now have a grid layout above them so that we can add the other widgets.
I'm adding the two labels first. And I'm calling the first one "New shapefile" and the second one "Field for name." And then I'm adding the two line edit widgets like that. And finally, it's two buttons for the first row. And that's pretty much it.
We have now created the gui for the dialog box. We're just going to save it, and now we're done. So we have created two forms, one called gui_main and one gui_newshapefile, and these can now be compiled into Python code and then used to create the key tool to combine it with a code that actually implements all the functionality needed, and that's what you're going to see in the rest of this walk through.