Our next goal is to initialize the widgets in the dialog. To repeat: We want all the topping checkboxes to be selected, the extra checkbox to be selected and the Junior size to be chosen. And finally, we want to check the extra cheese checkbox on Mondays, but only on Mondays.
Initialization is usually done in a constructor, and our pizza order entry application makes no difference. As before, we will not touch the code generated by uic, because this code would be lost the next time we run uic. So we are in for another addition to our derived class PizzaEntryImpl. So far, the constructor was empty, now we will fill it with content.
The task at hand is not very difficult, but we need to distinguish two different cases here: Checking the various toppings and selecting the Family size is static, we always want to do that. But checking the Extra Cheese checkbox on Mondays is not static, we need to compute the day of the week first. Bur for now, let's start with the simple case, the static selections.
Whether a checkbox is checked or not is a property of the checkbox, and the same goes for radiobuttons. So we can just use the property editor for setting these properties: Click on each of the toppings checkboxes one by one and set the value of the property checked to true by selecting true from the combobox in the value column. Do the same for the Family radiobutton. That's it.
Now for the extra cheese checkbox. Since we said that we do not know before runtime whether the checkbox should be checked or not when the pizza entry form is shown, we cannot use the property editor, but have to do this programmatically. In theory, all we need to do is see whether we have a Monday, call setChecked() on the extra cheese checkbox and passing the desired value. The question is how we get access to this widget.
Of course, situations like these are very common which is why Qt Designer provides for a very simple solution: All the widgets are represented as pointers with public access in the generated base class. All you need to do is find out the correct widget pointer and call setChecked() on it.
If you check the generated file PizzaEntry.h, you see that the widget pointers have non-obvious names like CheckBox1 or RadioButton3. You might wish that you had assigned the name properties of all the widgets sensible names, and in fact it is not too late to do that: You could just load the file pizza.ui into Qt Designer, change all name properties, save the file and regenerate PizzaEntry.h and PizzaEntry.cpp.
On the other hand, this is just a simple example, so we might get away with just guessing or checking in the designer which variable represents the extra cheese checkbox. Here is what we guessed — the name might be slightly different for you depending on the order in which you originally inserted the widgets:
#include <qcheckbox.h> #include <qradiobutton.h> PizzaEntryImpl::PizzaEntryImpl( QWidget* parent, const char* name, bool modal, WFlags f ) : PizzaEntry( parent, name, modal, f ) { QDate date = QDate::currentDate(); if( date.dayOfWeek() == 1 ) CheckBox5->setChecked( true ); else CheckBox5->setChecked( false ); } |
As you can see, we use the class QDate and its methods currentDate() and dayOfWeek() to find out whether the current day is a monday. Based on this information, we either call setChecked( true ) or setChecked( false ).
Add this code to your file PizzaEntryImpl.cpp and don't forget to remove the empty constructor implementation from PizzaEntryImpl.h. Compile your program with exactly the same commands as in the previous section and run it. The widgets should now be initialized as specified.