5.3. Standard Dialog Boxes
KDE offers several dialog boxes for common tasks, as described in Table 5.2.
Table 5.2. Dialog Boxes Available for Common Tasks
Other dialog boxes are available, but they are more complex or more specialized and thus are discussed elsewhere (see Chapters 6, 8, and 10).
Using these dialog boxes makes it easier to perform the common tasks of requesting file names, fonts, and so on from the user. More importantly, it makes providing answers to these questions for your application the same as for any other. Thus, if you use these dialog boxes, the user will have less new material to learn to use your application.
In the following program (Listing 5.15), named kstandarddialogs by the call to KApplication on line 10, you see how easily the dialog boxes previously listed can be used. They all provide static methods for retrieving precisely the information you are interested in without instantiating the class, but they may also be subclassed so that their functionality can be altered or extended.
Example 5.15. main.cpp: Contains a main() Function That Demonstrates Various KDE and Qt Dialog Boxes
You have used all these dialog boxes from main(). No need exists to create a main widget or even start the application. All these dialog boxes are modal, which means that they have their own local event loops. (Before you can display a window, you still need to create a KApplication so that it can perform some initialization, however.)
Each of the dialog boxes, except for KMessageBox, works in basically the same way. You can call a static method to start the dialog box and you are given back the object selected by the user.
KFileDialog has three static methods that you will often find useful:
These methods return a QString containing the filename or an empty string if no filename was chosen (that is, the user clicked the Cancel button). See line 18 for usage and Figure 5.11 for a screen shot of the file selector dialog box.
The method KFontDialog::getFont(), used on line 30, takes a QFont object as an argument and fills it with the font chosen by the user. If the user cancels the operation, getFont() returns the constant Qt::Rejected. See Figure 5.12 for a screen shot of the font selector dialog box.
The color selector works just like the font selector. The method KFontDialog::getColor(), used on line 42, takes a QColor object as an argument and fills it with the color chosen by the user. The method returns Qt::Rejected if the user clicks the Cancel button (see Figure 5.13).
KMessageBox can display several types of messages:
Displays a warning dialog box and gives the user the option to continue with the operation or cancel it. The final argument to this method is the text string that will be placed on the Continue button. In this sample program, you ask users whether they would really like to see the demo, and you put See Demo on the Continue button.
Tells the user something informative, but not related to an error (see Figure 5.15)
Tells the user that some requested action is not allowed (see Figure 5.16).
Tells the user that some error has occurred in the execution of a task (see Figure 5.17).