2.1 derivative dialog box class (subclassing qdialog)

Source: Internet
Author: User
Tags emit

2.1 derivative dialog box class (subclassing qdialog)

The first example is a Search dialog box implemented in C ++. The dialog box is implemented as a class, which is an independent control and has its own signal and slot functions.

The source code of the class is put in finddialog. h and finddialog. cpp respectively. First, check the finddialog. H code.

1 # ifndef finddialog_h
2 # define finddialog_h
3 # include <qdialog>
4 class qcheckbox;
5 class qlabel;
6 class qlineedit;
7 class qpushbutton;
8 class finddialog: Public qdialog
9 {
10 q_object
11 public:
12 finddialog (qwidget * parent = 0 );
13 signals:
14 void findnext (const qstring & STR, QT: casesensiti1_cs );
15 void findprevious (const qstring & STR, QT: casesensiti1_cs );
16 private slots:
17 void findclicked ();
18 void enablefindbutton (const qstring & text );
19 private:
20 qlabel * label;
21 qlineedit * lineedit;
22 qcheckbox * casecheckbox;
23 qcheckbox * backwardcheckbox;
24 qpushbutton * findbutton;
25 qpushbutton * closebutton;
26 };

27 # endif

A total of 27 lines, lines 1, 2, and 27 are used to prevent header files from being contained multiple times.
Row 3 contains the qdialog header file. This class inherits from qdialog and qdialog inherits from qwidget.
Rows 4th to 7 are used to declare the class forward in QT. Through the Forward Declaration, the compiler will know that this class already exists without writing the contained header file. This question will be discussed later.
Lines 8th to 26 are definitions of the finddialog class.
Row 3, q_object is a macro definition. IF signal or slots is used in the class, the macro must be declared.
Row 3, finddialog (qwidget * parent = 0); constructor is the standard format of the QT control class. The default parent parameter is null, indicating that there is no parent control.
Line 3, signal declares the two signals sent in this dialog box. If you select forward lookup, the dialog box sends the findprevious () signal. Otherwise, the findnext () signal is sent. Signal is also a macro. Before compilation, C ++ preprocessing turns it into a standard C ++ code. Qt: casesensititive is an enumeration type with QT: casesensitive and QT: caseinsensitive values.

In the private part of the class, two slot functions are declared. To implement these two functions, you need to use the information of other controls in the dialog box, so some control pointers are saved. The slot keyword, like signal, is also a macro.

For private member variables, we only use their pointers and do not access them. The Compiler does not need to know their detailed definitions, so it only uses the Forward Declaration of these classes. Of course, you can also use <qcheckbox>, <qlabel>, etc. However, using the Forward Declaration will make compilation faster.

The finddialog. cpp source file code is as follows:

File Header and constructor

1 # include <qtgui>
2 # include "finddialog. H"
3 finddialog: finddialog (qwidget * parent)
4: qdialog (parent)
5 {
6 label = new qlabel (TR ("find & what :"));
7 lineedit = new qlineedit;
8 label-> setbuddy (lineedit );
9 casecheckbox = new qcheckbox (TR ("Match & Case "));
10 backwardcheckbox = new qcheckbox (TR ("Search & backward "));
11 findbutton = new qpushbutton (TR ("& find "));
12 findbutton-> setdefault (true );
13 findbutton-> setenabled (false );
14 closebutton = new qpushbutton (TR ("close "));
15 connect (lineedit, signal (textchanged (const qstring &)),
16 this, slot (enablefindbutton (const qstring &)));
17 connect (findbutton, signal (clicked ()),
18 this, slot (findclicked ()));
19 connect (closebutton, signal (clicked ()),
20 This, slot (close ()));
21 qhboxlayout * topleftlayout = new qhboxlayout;
22 topleftlayout-> addwidget (Label );
23 topleftlayout-> addwidget (lineedit );
24 qvboxlayout * leftlayout = new qvboxlayout;
25 leftlayout-> addlayout (topleftlayout );
26 leftlayout-> addwidget (casecheckbox );
27 leftlayout-> addwidget (backwardcheckbox );
28 qvboxlayout * rightlayout = new qvboxlayout;
29 rightlayout-> addwidget (findbutton );
30 rightlayout-> addwidget (closebutton );
31 rightlayout-> addstretch ();
32 qhboxlayout * mainlayout = new qhboxlayout;
33 mainlayout-> addlayout (leftlayout );
34 mainlayout-> addlayout (rightlayout );
35 setlayout (mainlayout );
36 setwindowtitle (TR ("find "));
37 setfixedheight (sizehint (). Height ());
38}

Here, the finddialog constructor is complete. We use new when uploading controls and layout. In general, we also need to write the Destructor Delete controls.
However, this is not required in QT. When the parent control is destroyed, QT automatically deletes all its child controls and la S.

The following are two slot functions of the finddialog class:
39 void finddialog: findclicked ()
40 {
41 qstring text = lineedit-> text ();
42 QT: casesensiti1_cs =
43 casecheckbox-> ischecked ()? Qt: casesensitive
44: QT: caseinsensitive;
45 if (backwardcheckbox-> ischecked ()){
46 emit findprevious (text, CS );
47} else {
48 emit findnext (text, CS );
49}
50}
51 void finddialog: enablefindbutton (const qstring & text)
52 {
53 findbutton-> setenabled (! Text. isempty ());
54}
When you click the findbutton button, findclicked () is called. Based on the status of backwardcheckbox, The findprevious () or findnext () signal is sent. Emit is also a QT macro.
When the user changes the text in lineedit, The enablefindbutton () slot function is called. If the text is entered, make findbutton valid; otherwise, it will be invalid.

Finally, create the main. cpp test finddialog dialog box.
1 # include <qapplication>
2 # include "finddialog. H"
3 int main (INT argc, char * argv [])
4 {
5 qapplication app (argc, argv );
6 finddialog * dialog = new finddialog;
7 dialog-> show ();
8 return app.exe C ();
9}
Run the qmake Compiling Program. Because the finddialog contains the q_object macro, The makefile generated by qmake will run MOC (the original object compiler of QT) with special rules ).
To ensure that MoC works properly, the class definition must be placed in the header file rather than in the implementation file. The code generated by MOC contains this header file and adds the self-implemented C ++ code.
The class that uses the q_object macro must run MOC. If qmake is used, the makefile automatically contains relevant rules. If you forget to run MOC, a connection error occurs. Different compilers provide different prompts, and some of them will be very obscure. The GCC error message is as follows:
Finddialog. O: In function 'finddialog: TR (char const *, char const *)':
/Usr/lib/QT/src/corelib/global/qglobal. h: 1430: Undefined reference
'Finddialog: staticmetaobject'

The output in Visual C ++ is as follows:

Finddialog. OBJ: Error lnk2001: unresolved external symbol
"Public :~ Virtual int _ thiscall myclass: qt_metacall (Enum qmetaobject
: Call, Int, void **)"
In this case, you need to re-run qmake, update makefile, and then compile the program.
Run the program. If you see the shortcut key, test Alt + W, ALT + C, ALT + B, ALT + F to trigger the corresponding processing program. Use the tab key to change the focus to different controls. The default Tab key is the order in which controls are created. Qwidget: settaborder () can change this order.
Appropriate tab order and keys are provided to allow you to run programs without a mouse and quickly control programs through the keyboard.
 

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.