Chapter 8 Use of the Qt GUI dialog box, qtgui
Chapter 8 Use of the Qt GUI dialog box
A dialog box can be a modal or a non-modal model. When we face a dialog box (for example, select a file dialog box) in a user interface program before the operation is completed, other windows on the interface cannot be operated. This dialog box is a modal dialog box, when you use the search and replace dialog box in a word processing software, you can switch between the word processing software and the search and replace dialog box for interaction. This is a non-modal dialog box.
First, let's take a look at the inheritance relationship of the QDialog class, as shown in.
QDialog inherits from QWidget and is inherited by Qt's built-in dialog box class (QFileDialog select file or directory dialog box, QFontDialog select font dialog box, QMessageBox message prompt dialog box, and so on. To implement your own dialog box, you must inherit from the QDialog class and include the header file <QDialog>.
1. Implement your own dialog box class
Implement a Find (Search dialog box) and its running effect is shown in. This will implement a dialog box with autonomy.
The program list is as follows:
Finddialog. h
1 #ifndef FINDDIALOG_H 2 #define FINDDIALOG_H 3 4 #include <QDialog> 5 #include <QCheckBox> 6 #include <QLabel> 7 #include <QLineEdit> 8 #include <QPushButton> 9 10 class FindDialog : public QDialog11 {12 Q_OBJECT13 14 public:15 FindDialog(QWidget *parent = 0);16 ~FindDialog();17 18 signals:19 void findNext(const QString &str, Qt::CaseSensitivity cs);20 void findPrevious(const QString &str, Qt::CaseSensitivity cs);21 22 private slots:23 void findClicked();24 void enableFindButton(const QString &text);25 26 private:27 QLabel *label;28 QLineEdit *lineEdit;29 QCheckBox *caseCheckBox;30 QCheckBox *backwardCheckBox;31 QPushButton *findButton;32 QPushButton *closeButton;33 };34 35 #endif // FINDDIALOG_H
Finddialog. cpp:
1 # include <QtGui> 2 # include "finddialog. h "3 4 FindDialog: FindDialog (QWidget * parent) 5: QDialog (parent) 6 {7 label = new QLabel (tr (" Find & what ")); // The tr () function is a sign that translates them into other languages. "&" indicates the shortcut key (Alt + W) 8 lineEdit = new QLineEdit; 9 label-> setBuddy (lineEdit); // sets the row editor as the tag partner and receives the focus when pressing the tag shortcut key (Alt + W, focus will be moved to the row editor 10 11 caseCheckBox = new QCheckBox (tr ("Math & case"); 12 backwardCheckBox = new QCheckBox (tr ("Search & backw Ard "); 13 14 findButton = new QPushButton (tr (" & Find "); 15 findButton-> setDefault (true ); // set the "Find" button as the default button. The default button means that the user can press the corresponding button 16 findButton-> setEnabled (false) when entering ); // disable the "Find" button. It is usually displayed in gray and cannot interact with users. 17 18 closeButton = new QPushButton (tr ("Close ")); 19 20 connect (lineEdit, SIGNAL (textChanged (const QString &), this, SLOT (enableFindButton (const QString &); 21 connect (findButton, SIGNAL (clicked (), this, SLOT (findClicked (); 22 connect (closeButton, SIGNAL (clicked (), this, SLOT (close (); 23 24 QHBoxLayout * topLeftLayout = new QHBoxLayout; 25 topLeftLayout-> addWidget (label); 26 topLeftLayout-> addWidget (lineEdit); 27 28 rows * leftLayout = new QVBoxLayout; 29 leftLayout-> addLayout (topLeftLayout ); 30 leftLayout-> addWidget (caseCheckBox); 31 leftLayout-> addWidget (backwardCheckBox); 32 33 QVBoxLayout * RightLayout = new layout; 34 rightLayout-> addWidget (findButton); 35 rightLayout-> addWidget (closeButton); 36 rightLayout-> addStretch (); 37 38 minutes * mainLayout = new layout; 39 mainLayout-> addLayout (leftLayout); 40 mainLayout-> addLayout (rightLayout); 41 setLayout (mainLayout ); // install the mainLayout layout in FindDialog42 43 setWindowTitle (tr ("Find"); 44 setFixedHeight (sizeHint (). height (); 45} 46 47 void FindDialog: findClicked () 48 {49 QString text = lineEdit-> text (); 50 Qt: casesensiti1_cs = caseCheckBox-> isChecked ()? Qt: CaseSensitive: Qt: CaseInsensitive; 51 if (backwardCheckBox-> isChecked () 52 {53 emit findPrevious (text, cs ); 54} 55 else56 {57 emit findNext (text, cs); 58} 59} 60 61 void FindDialog: enableFindButton (const QString & text) 62 {63 findButton-> setEnabled (! Text. isEmpty (); 64} 65 66 FindDialog ::~ FindDialog () 67 {68 69}
Main. cpp
#include <QApplication>#include "finddialog.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); FindDialog *w = new FindDialog; w->show(); return a.exec();}
Compile and run the program.
2. Use the Qt designer Settings dialog box to achieve the following effects: