Continue to learn QT-QT (2) from the QT built-in dialog box)

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.