Tutorial: Adding Functionality To A Dialog By Subclassing

# Chapter 3. Tutorial: Adding Functionality To A Dialog By Subclassing

In this chapter, we will build on the pizza entry form presented in the last chapter and augment it. You will learn how to continue work on an already existing dialog and how to add functionality to the dialog classes that Qt Designer creates for you.

# Continuing Work On An Already Existing Dialog

Once you have run your application, you might notice that your dialog still needs work. This is no problem at all. All you have to do is start Qt Designer again (in case you have shut it down in the meantime) and load the `.ui` file via the menu entry File/Open or the load button on the toolbar (see Figure 3-1). If you use Windows or the K Desktop Environment on a Unix system, you can also drag an icon representing a `.ui` file from an Explorer/Konqueror window or the desktop onto the Qt Designer window and drop it there.

Figure 3-1. The load icon

You will see your form as you have left it. Qt Designer does not care about any generated source files; all it uses, both when saving and when loading, are the XML-based `.ui` files. [1]

This behavior of Qt Designer has one important consequence: Never change the files generated by uic! Any changes will be lost once you change the `.ui` file, save it, and rerun uic. If you need to change some of the generated code—e.g., because you want to add some functionality—you have to do this by subclassing. We'll see in a minute how to do that, but first let's define the objectives of this tutorial:

- The OK and Cancel buttons should actually close the dialog; when the OK button is pressed, a message box should inform the user that the order has been sent to the pizza place.

- Since our pizza service wants to have as much turnover as possible, we want all the toppings and the Family size to be selected by default. If you tried the dialog from the previous chapter, you might have noticed that no size was selected from the beginning, which of course is bad, since the customer might forget to select one.

- Turnover is pretty low on Mondays, so to increase the number of orders, our pizza service provides extra cheese without additional cost on Mondays. As a courtesy to the user, we want to check the extra cheese check box on Mondays but leave it unchecked on all other days.

We will now implement these features one by one.

### Notes

[1] | Or tmake project files, as you will learn in the section called Using tmake For Generating And Building Qt Designer Files in Chapter 5.

Putting It All Together |  | Adding Functionality To The Push Buttons