實例:查找對話框
包含三個文件,finddialog.h,finddialog.cpp及main.cpp。
//finddialog.h代碼
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget *parent = 0);
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
void findClicked();
void enableFindButton(const QString &text);
private:
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif
注釋:
class FindDialog : public QDialog表示繼承的對話框,在Qt中QDialog是一個常用到的類。
Q_OBJECT是一個宏,是信號槽所必須的,在使用信號槽時,在類的開始要使用這個宏,否則在編譯時候會出錯。
接下來定義了信號和槽,對於信號和槽以后會詳細介紹。
//finddialog.cpp代碼
#include <QtGui>
#include "finddialog.h"
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
connect(lineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(enableFindButton(const QString &)));
connect(findButton, SIGNAL(clicked()),
this, SLOT(findClicked()));
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());
}
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);
}
}
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
在finddialog.cpp中主要就是實現了信號與槽的關聯以及布局管理。這里主要說明一下布局管理。
Qt提供了在窗口組件上管理子窗口組件的管理方式:
1.絕對位置方式:通過基類QWidget提供的setGeometry() 設置子窗口組件的大小及其在父窗口的位置。缺點:不能調整子窗口大小,不能隨父窗口大小改變而改變,子窗口大小和文本可能會被截斷,程序員需要不斷計算子窗口大小和位置。
2.手工布局方式:也是通過基類QWidget提供的setGeometry() 設置子窗口組件的大小及其在父窗口的位置。不過它與第一種不同,它是通過重載QWidget::resizeEvent(QResizeEvent*)函數來實現。當父窗口改變時子窗口會做出相應改變。但是它仍然需要程序員手工計算。
3.布局管理器方式:這是最好的Qt布局管理方式,它主要提供了4中方式:
(1)水平布局管理器:QHBoxLayout,按水平方向組織窗口組件
(2)垂直布局管理器:QVBoxLayout,按垂直方向組織窗口組件
(3)網格布局管理器:QGridLayout,按二維網格方式組織窗口組件
(4)棧布局管理器:QStackedLayout,按照類似於棧的方式組織窗口組件,在某一時刻只有一個窗口組件是可見的,Qt沒有提供對該布局管理器的支持,但是提供了一個棧部件,QStackedWidget,可以使用它來實現棧布局管理器。
在上面的例子中,使用其中的兩個布局管理器,水平和垂直布局管理器。下面的圖是以上代碼布局管理后的一個圖

QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
首先創建水平布局,然后再在最上面左邊添加兩個組件:label,lineEdit
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
然后又創建垂直布局,然后再把上面的水平布局添加到該垂直布局的最上面,接着添加兩個組件:caseCheckBox,backwardCheckBox。
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch();
接着創建了一個垂直布局,添加兩個組件:findButton,closeButton,之后加入了拉伸系數,也可以叫彈簧,因為就像彈簧一樣把這些組件頂起來,可以試試在findButton和closeButton直接加入彈簧看看是什么結果。
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
最后,又創建了一個水平布局,在這個水平布局中添加了前面創建的后兩個布局:水平布局和垂直布局。之后就設置布局管理器,setLayout(),參數是布局管理器名。
布局管理器就說這么多,后面還會提到。布局管理器在Qt中非常實用。
下面是該實例的總體類關系圖:

最后的是mian代碼
// 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();