build dialogs using Qt Designer, Qt's visual design tool. Using Qt Designer is a lot faster than hand-coding and makes it easy to test different designs and to change designs later.
a Find dialog:We will implement the dialog as a class
We will start with finddialog.h.
1 #ifndef FINDDIALOG_H
2 #define FINDDIALOG_H
3 #include <QDialog>
4 class QCheckBox;
5 class QLabel;
6 class QLineEdit;
7 class QPushButton;// A forward declaration tells the C++ compiler that a class exists,
8 class FindDialog : public QDialog
9 {
10 Q_OBJECT //The Q_OBJECT macro is necessary for all classes that define signals or slots.
11 public:
12 FindDialog(QWidget *parent = 0);//The default is a null pointer, meaning that the dialog has no parent.
13 signals:
14 void findNext(const QString &str, Qt::CaseSensitivity cs);
15 void findPrevious(const QString &str, Qt::CaseSensitivity 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;
/*For the private variables, we used forward declarations of their classes. This was possible because they are all pointers and we don't access them in the header file, so the compiler doesn't need the full class
definitions.*/
26 };
27 #endif
We will now look at finddialog.cpp,
1 #include <QtGui>
//a header file that contains the definition of Qt's GUI classes.The <QtGui> header file contains the definition
//of all the classes that are part of the QtCore and QtGui modules.
2 #include "finddialog.h"
3 FindDialog::FindDialog(QWidget *parent)
4 : QDialog(parent)
5 {
6 label = new QLabel(tr("Find &what:"));
//a good habit to surround user-visible strings with tr(),translation to other languages.
7 lineEdit = new QLineEdit;
8 label->setBuddy(lineEdit);
/*6 create a label with a shortcut key (Alt+W),create a label with a shortcut key (Alt+W),A buddy is a widget that accepts the focus when the label's shortcut key is pressed. So when the user presses Alt+W (the label's shortcut), the focus goes to the line editor (the label's buddy)*/
9 caseCheckBox = new QCheckBox(tr("Match &case"));
10 backwardCheckBox = new QCheckBox(tr("Search &backward"));
11 findButton = new QPushButton(tr("&Find"));//use ampersands ('&') to indicate shortcut keys.Alt+F
12 findButton->setDefault(true);
/*we make the Find button the dialog's default button by calling setDefault(true). The default button is the button that is pressed when the user hits Enter.*/
13 findButton->setEnabled(false);//a widget is disabled,shown grayed out and will not respond to user interaction.
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();/*It uses up the empty space below the Find and Close buttons, ensuring that
these buttons occupy the top of their layout.*/
32 QHBoxLayout *mainLayout = new QHBoxLayout;
33 mainLayout->addLayout(leftLayout);
34 mainLayout->addLayout(rightLayout);
35 setLayout(mainLayout);
36 setWindowTitle(tr("Find"));
37 setFixedHeight(sizeHint().height());//QWidget::sizeHint() function returns a widget's "ideal" size.
38 }
39 void FindDialog::findClicked()//is called when the user clicks the Find button.
40 {
41 QString text = lineEdit->text();
42 Qt::CaseSensitivity 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)//called when the user changes the text in the line editor.
52 {
53 findButton->setEnabled(!text.isEmpty());
54 }
create a main.cpp file to test our FindDialog widget:
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.exec();
9 }