今天说的关于QT的一个练习,写一个搜索对话框吧!好吧,直接进入主题吧!先建工程,需要注意的是在Base Dialog的选择的时候要选QDialog。其他的没什么了。名字的话就随便了,我起了个dialog。
1、首先打开dialog.h,编写头文件,代码如下:
#ifndef DIALOG_H
#define DIALOG_H
#include <QtGui/QDialog>
class QCheckBox; //声明四个用到的类
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog //FindDialog继承QDialog这个类
{
Q_OBJECT //一个宏,凡是定义信号槽的类都必须声明这个宏
public:
FindDialog(QWidget *parent = 0);
~FindDialog();
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 // FINDDIALOG_H2、再打开dialog.cpp文件,进行编写,代码如下:
#include <QtGui>
#include "dialog.h" //把头文件引进来
FindDialog::FindDialog(QWidget *parent): QDialog(parent)
{
label = new QLabel(tr("Find &what:")); //label传进来字符串
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case")); //创建两个QCheckBox
backwardCheckBox = new QCheckBox(tr("Search &backford"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true); //这两行代码就是先把button设置为灰色不可用
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
//接下来就是三个connect语句了,用来连接信号槽
connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&))); //当lineEdit发出textChanged信号时,FindButton的enableFindButton函数就被调用
connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
//接下来就是layout的使用了,这个大家仔细琢磨琢磨就知道了
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());
}
FindDialog::~FindDialog()
{
}
void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
if(backwardCheckBox->isChecked())
{
emit findPrevious(text, cs);//如果backwardCheckBox被选中的话,就发出findPrevious信号
}
else {
emit findNext(text, cs);
}
}
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
3、最后就是main.cpp文件了,代码如下:
#include <QApplication>
#include "dialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
最后的运行效果如下:
![](https://img-my.csdn.net/uploads/201302/08/1360314124_9174.png)