对话框是图像用户界面的重要构成部分,它为用户提供许多选项和多种选择。我们先通过书上例子(Find对话框)手打程序熟悉下创建对话框的过程。
0.头文件 finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
//防止对头文件的多重包含
#include <QDialog>
#include <Qlabel>
#include <Qcheckbox>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
//前置声明(forward declaration)
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
//定义FindDialog,并让它成为QDialog的子类
class FindDialog : public QDialog
{
//对于所有定义了信号和槽的类,在类定义开始处的Q_OBJECT宏都是必须的
Q_OBJECT
public:
//构造函数:parent参数指定了它的父窗口参数
FindDialog(QWidget *parent = 0); //此处parent=0意味着该对话框没有父对象
signals:
//声明当用户单击Find按钮时对话框发射的信号
void findNext(const QString &str,Qt::CaseSensitivity cs); //向后查询(search forward) : findNext()
void findPrevious(const QString &str,Qt::CaseSensitivity cs); //向前查询(search backward) : findPrevious()
private slots:
//private段声明两个槽
void findClicked();
void enableFindButton(const QString &text);
private:
//对于以下私有变量,我们使用类前置类型
//因为他们都是指针,没必要在头文件中就去访问,所以此处无需这些类的完整定义。
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif // FINDDIALOG_H
1.FindDialog的实现——finddialog.cpp
#include "finddialog.h"
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent) //将parent参数传递给基类的构造函数
//子类创建前先要构建父类,此处使用父类中带参数的构造方式QDialog(parent)构造了一个父类对象。
{
label = new QLabel(tr("Find &what")); //tr()函数调用是把它们翻译成其他语言的标记
lineEdit = new QLineEdit;
//将行编译器设置为标签的伙伴(buddy),即作为按下标签键时的接受焦点
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true); //将Find按钮成为对话框的默认按钮
findButton->setEnabled(false); //禁用Find按钮
closeButton = new QPushButton(tr("Close"));
//QObject是FindDialog父对象之一,故可以省略connect()函数前的QObject::
//当行编辑器中的文本发生变化时,调用私有槽enableFindButton
connect(lineEdit,SIGNAL(textChanged(const QString &)),
this,SLOT(enableFindButton(const QString &)));
//用户单击Find按钮时,会调用findClicked()私有槽
connect(findButton,SIGNAL(clicked()),
this,SLOT(findClicked()));
//用户单击Close按钮时,关闭对话框
connect(closeButton,SIGNAL(clicked()),
this,SLOT(close()));
//使用布局管理器摆放子窗口部件
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch(); //伸展器,用于占据空白区域
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}
//单击Find按钮时调用findClicked()槽
//根据Search backward选项决定发射findPrevious() or findNext()
void FindDialog::findClicked()
{
QString text = lineEdit->text();
//区分向前和向后查询中字符串大小写敏感问题
Qt::CaseSensitivity cs =
caseCheckBox->isChecked() ?Qt::CaseSensitive
:Qt::CaseInsensitive;
if(backwardCheckBox->isChecked()){
emit findPrevious(text,cs);
}else{
emit findNext(text,cs);
}
}
//如果行编辑器文本被改变则调用enableFindButton()槽。
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
2.创建main.cpp来运行对话框
#include <QApplication>
#include "finddialog.h"
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
运行结果:
这个对话框目的只是了解对话框的构造过程,接下来我们就会去具体实现其信号-槽的功能。
(等待施工。。。)