Qt has rich built-in dialogs, such as color settings, font settings, and file selection. Let's take a look at the overall interface:
Next, let's take a look at the implementation code of the color selection dialog box:
1: qpalette palette = displaytextedit-> palette ();/* Obtain the palette in textedit */
2:
3:/* Open a color dialog box and obtain the selected color */
4:ConstQcolor & color =
5: qcolordialog: getcolor (palette. color (qpalette: base ),This);
6:If(Color. isvalid ()){
7: palette. setcolor (qpalette: Base, color );/* Set the color */
8: displaytextedit-> setpalette (palette );/* Apply it to qtextedit */
The effect is as follows:
Next let's take a look at the implementation of the font dialog box.Code:
1:BoolOK;/* The record font dialog box determines whether the button is pressed */
2:
3:/* Record the font selected in the font dialog box */
4:ConstQfont & font = qfontdialog: getfont (& OK,
5:This-> Displaytextedit-> font (),
6:This,
7: TR ("Font dialog box"));
8:If(OK ){
9:/* Apply the selected font to qtextedit */
10:This-> Displaytextedit-> setfont (font );
11 :}
The effect is as follows:
Finally, let's look at an interesting progress dialog box:
1:/* Create a progress dialog box and set its initial value to 0 and maximum value to 10000 */
2: qprogressdialog progress (TR ("Copying file..."),
3: TR ("Cancel"),
4: 0,
Five: 10000,
6:This);
7: progress. setwindowmodality (QT: windowmodal );
8: progress. setwindowtitle (TR ("Progress dialog box"));
9: progress. Show ();
10:For(IntI = 0; I <10001; I ++ ){
11: progress. setvalue (I );/* Set the SS Description */
12: qapp-> processevents ();
13:If(Progress. wascanceled ()){/* Click the cancel button */
14:Break;
15 :}
16 :}
:
These built-in dialogs can save us a lot of time.