Qt Study Notes (4) dialog box-QT built-in dialog box

Source: Internet
Author: User
Tags getcolor

In the previous section, we have clearly understood the sub-classes of qdialog, such as qcolordialog and qfontdiog. These dialogs are called built-in dialogs and standard dialogs.

This section describes how to use the standard dialog box.

1. Main Interface Designed for us

The drawing of the window is not described here.

Note: I did not use the layout manager to draw the window, but I used my own "design" layout.

 
Resize (320,240 );

First, I callQwidget::Resize(IntW,IntH)
To set the dialog box window size.

Then design other components

 
// Custom Layout infotextedit-> setgeometry (qrect (20,130,280,100); colorbtn-> setgeometry (qrect (20, 10, 80, 30 )); errorbtn-> setgeometry (qrect (120, 10, 80, 30); filebtn-> setgeometry (qrect (220, 10, 80, 30 )); fontbtn-> setgeometry (qrect (20, 50, 80, 30); inputbtn-> setgeometry (qrect (120, 50, 80, 30 )); pagebtn-> setgeometry (qrect (220, 50, 80, 30); progressbtn-> setgeometry (qrect (20, 90, 80, 30 )); printbtn-> setgeometry (qrect (120, 90, 80, 30 ));

Mainly calledQwidget::Setgeometry(IntAx,IntAy,IntAw,IntAh)

Of course, we also need to edit the button "signal and slot"

You can design a slot function and then internally identify the button objects.

When the signal is activated, the applicationProgramWill enter the slot function. We can use sender () to send signals to objects.

 
Qpushbutton * BTN = qobject_cast <qpushbutton *> (sender ());

T Qobject_cast ( Qobject * Object )

After the type conversion is completed, convert the <qobject *> type object pointer to the <t *> type object pointer. If the conversion is successful, the correct Object Pointer is returned. Otherwise, 0 is returned.

------------------------------------------------

Returns the givenObjectCast to type T if the object is of type T (or of a subclass); Otherwise returns 0. IfObjectIs 0 then it will also return 0.

The class T must inherit (directly or indirectly) qobject and be declared
With the q_object macro.

------------------------------------------------

Type T must be directly inherited or indirectly inherited from the qobject class, and there is a q_object macro variable in the definition of this class (otherwise, qobject_castThe returned value is undefined)

2. Color dialog box


The followingCodeIs the application of the color dialog box

 
Qpalette palette = infotextedit-> palette (); // obtain the color palette object for text editing. // qcolor color = qcolordialog: getcolor (qpalette: Base, this ); // The initial color value of the color dialog box is the background color qcolor color = qcolordialog: getcolor (palette. color (qpalette: base), this); // if you click Cancel in the color dialog box, the resulting color is invalid if (color. isvalid () {// qpalette: base is usually used for background color palette. setcolor (qpalette: Base, color); infotextedit-> setpalette (palette );}

The color palette is used to set the background color and foreground color of widgets, button widgets, and text input Widgets. It is specified by colorrole and is described in QT documents.

3. error message box

Qerrormessage msgbox (this); // This is mainly used to distinguish "Display message" msgbox in the error message box. setwindowtitle (TR ("error message box"); msgbox. showmessage (TR ("error message box 1"); msgbox. showmessage (TR ("error message box 1"); msgbox. showmessage (TR ("error message box 1"); msgbox. showmessage (TR ("error message box 2"); msgbox. showmessage (TR ("error message box 3"); msgbox.exe C ();

We can create a qerrormessage object, set the display content separately, and finally let the dialog box be executed.

If we don't need exec (), but directly show (), then we will find a pop-up error message box.

Because the msgbox lifecycle ends after show (), we still call exec (). The application will return after the user closes the message dialog box.

This is inappropriate ~~ Limited language capabilities ~~ This can only be explained.

Iv. File Dialog Box

 
Qstring filename = qfiledialog: getopenfilename (this, TR ("Open File"), "/Home", TR ("any file (*. c) "); infotextedit-> settext (filename );

The file dialog box is easy to create.Qfiledialog::The third parameter of getopenfilename () is the initial default path, and the fourth parameter is the filter. The file format specified by the filter is valid.

5. Font dialog box

 
Bool OK; qfont font = qfontdialog: getfont (& OK, infotextedit-> font (), this, TR ("font dialog box"); If (OK) {infotextedit-> setfont (font );}

When we select the font format to be set and click OK, the value of bool OK is true.

Vi. Input dialog box

 
Bool OK; qstring text = qinputdialog: gettext (this, TR ("input dialog box"), TR ("input text"), qlineedit: normal, TR ("fuck"), & OK); // judge if (OK &&! Text. isempty () {infotextedit-> settext (text );}

The input dialog box is not described. You can also define your own input dialog box class.

7. Page Setting Dialog Box

 
Qprinter printer; qpagesetupdialog pagedlg (& printer, this); pagedlg. setwindowtitle (TR ("Page Setting Dialog Box"); if(pagedlg.exe C () = qdialog: accepted) {// next step}

We can all view the help documentation of QT to complete these program designs.

8. Progress dialog box

Qprogressdialog progressdlg (TR ("copying files"), TR ("cancel"), 0, 10000, this); progressdlg. setwindowtitle (TR ("progress bar"); // set it to the modal dialog box progressdlg. setwindowmodality (QT: windowmodal); // if you use the exec progress dialog box, the main progressdlg is displayed. show (); For (INT I = 0; I <10000; I ++) {progressdlg. setvalue (I); qapp-> processevents (); If (progressdlg. wascanceled () {break;} progressdlg. setvalue (10000 );

We can call qprogressdialog ::Setvalue(IntSS) To set the progress.

In the progress dialog box, we must show (). If exec () is called, the interface will get stuck ~. Note the use of show () and exec ~~

 
Qapp-> processevents ();

Pay attention to the above functions. If I block this function, the interface will be stuck.

The explanation is as follows:

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Process the event.

For example, if a transaction is time-consuming, You can process events () from time to time in the middle so that the interface can process events and avoid seemingly unresponsive events.

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IX,

Qprinter printer; qprintdialog printdlg (& printer, this); printdlg. setwindowtitle (TR ("Print dialog box"); if(printdlg.exe C () = qdialog: accepted ){}

10. Const variable reference

Eg:

Void fun (const qstring & Str );

STR is a reference to a constant object. Therefore, you can pass in a temporary qstring object as a real parameter. However, for reference to a non-constant object, an unspecified object, a temporary object, and a specific value cannot be used as a real parameter.


Add the program hereSource codeDownload link: http://download.csdn.net/detail/fzu_dianzi/3743682

The above are purely personal study notes. If anything goes wrong, I hope to raise it. We hope to study and make progress together. My email address is: xzy@yingzhi8.com

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.