// 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 findPrev(const QString &str,Qt::CaseSensitivity cs);
private slots://声明槽
void findClicked();
void enableFindButton(const QString &str);
private:
QLabel *lable;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findBtn;
QPushButton *closeBtn;
};
#endif
// finddialog.cpp
#include <QtGui>
#include "finddialog.h"
FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
this->lable = new QLabel(tr("Find &what:"),this);
this->lineEdit = new QLineEdit(this);
lable->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"),this);
backwardCheckBox = new QCheckBox(tr("Search &backward"),this);
findBtn = new QPushButton(tr("&Find"),this);
findBtn->setDefault(true);
findBtn->setEnabled(false);
closeBtn = new QPushButton(tr("Close"),this);
QObject::connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));
QObject::connect(findBtn,SIGNAL(clicked()),this,SLOT(findClicked()));
QObject::connect(closeBtn,SIGNAL(clicked()),this,SLOT(close()));
QHBoxLayout *topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(lable);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout *rightLayout = new QVBoxLayout;
rightLayout->addWidget(findBtn);
rightLayout->addWidget(closeBtn);
rightLayout->addStretch(1);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->setMargin(11);
mainLayout->setSpacing(5);
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
this->setWindowTitle(tr("Find"));
this->setFixedHeight(sizeHint().height());
}
void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
emit findPrev(text,cs);//发射"向前搜索信号"
else
emit findNext(text,cs);//发射"向后搜索信号"
}
void FindDialog::enableFindButton(const QString &str)
{
findBtn->setEnabled(!str.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();
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/05/03/735019.html,如需转载请自行联系原作者