Initializing The State Of Widgets In The Form

Our next goal is to initialize the widgets in the dialog. To repeat: We want all the toppings check boxes to be checked and the Family size to be chosen. And finally, we want to check the extra cheese check box on Mondays, but only on Mondays.

Initialization is usually done in a constructor, and our pizza order-entry application is no different. 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 has been 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 check box on Mondays is not static; we need to compute the day of the week first. But for now, let's start with the simple case, the static selections.

Whether a check box is checked or not is a property of the check box, and the same goes for radio buttons. So we can just use the property editor for setting these properties: Click on each of the toppings check boxes one by one and set the value of the property checked to true by selecting true from the combo box in the value column. Do the same for the Family radio button. That's it.

Now for the Extra Cheese check box. Since we said that we do not know before runtime whether the check box 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 determine whether it is a Monday, call setChecked() on the Extra Cheese check box, and pass the desired value. The question is, how do we get access to this widget?

Of course, situations like this are very common, which is why Qt Designer provides 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 will 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 Qt Designer which variable represents the Extra Cheese check box. 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>
#include <qdatetime.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 );
    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.