-
FINDDIALOG_H 头文件:
#ifndef FINDDIALOG_H #define FINDDIALOG_H #include <QtGui/QDialog> /*声明四个用到的类*/
class QCheckBox; class QLabel; class QLineEdit; class QPushButton; /*首先定义一个派生类QDialog,基类是FindDialog*/
class FindDialog : public QDialog {
/*Q_OBJECT 是宏,定义信号槽的类都要声明这个宏*/ Q_OBJECT /*构造函数和析构函数*/
public: FindDialog(QWidget *parent = 0); ~FindDialog(); /*signals,QT关键字,对信号定义.FindDialog 有两个public 的信号,它可以在特定的时刻发出这两个信号,就这里来说,如果用户点击了Find 按钮,并且选中了 Search backward ,就会发出 findPrevious() ,否则发出findNext()。*/
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_H
-
FindDialog.cpp 函数:
/*QtGui,它包括了QtCore 和 QtGui 模块。是统称,在正式开发的时候用到哪些组件,使能哪些组件*/
#include <QtGui>
#include "finddialog.h"
/*在类的外面定义派生类狗在函数*/
FindDialog::FindDialog(QWidget *parent): QDialog(parent) { label = new QLabel(tr("Find &what:")); lineEdit = new QLineEdit; label ->setBuddy(lineEdit);
/*创建了两个 QCheckBox,把默认的按钮设为 findButton,把 findButton 设为不可用——也就是变成灰色的了。*/
caseCheckBox = new QCheckBox(tr("Match &case")); backwardCheckBox = new QCheckBox(tr("Search &backford")); findButton = new QPushButton(tr("&Find")); findButton->setDefault(true); findButton->setEnabled(false); closeButton = new QPushButton(tr("Close"));
/*connect 语句,用来连接信号槽,当 lineEdit 发出textChanged(const QString&) 信 号 时, FindDialog 的 enableFindButton(const QString&)函数会被调用。
注意,connect()函数也是 QObject 的,因为我们继承了 QObject,所以能够直接使用。
*/
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());
//设置对话框高度,这里调用sizeHint().height())函数为最理想高度
}
/*槽函数*/
FindDialog::~FindDialog() { } /*槽函数,首先取出 lineEdit 的输入值;然后判断 caseCheckBox是不是选中。如果 backwardCheckBox 被选中,就 emit(发出)信号 findPrevious(),否则 emit 信号 findNext。*/
void FindDialog::findClicked() { QString text = lineEdit->text(); Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive; if(backwardCheckBox->isChecked()) { emit findPrevious(text, cs); } else { emit findNext(text, cs); } } void FindDialog::enableFindButton(const QString &text) { findButton->setEnabled(!text.isEmpty());
}
- 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(); }
时间: 2024-10-18 14:32:15