您的位置:首页 > 编程语言 > Qt开发

QT的FindDialog

2013-08-09 09:43 671 查看
#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* pParents = 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& str);
private:
QLabel* label;
QLineEdit* lineEdit;
QCheckBox* caseCheckBox;
QCheckBox* backwardCheckBox;
QPushButton* findButton;
QPushButton* closeButton;
};
#endif // FINDDIALOG_H
#include<QtGui>
#include"FindDialog.h"
FindDialog::FindDialog(QWidget* pParent)
{
label = new QLabel(tr("Find &what"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Math &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 &str)
{
findButton->setEnabled(!str.isEmpty());
}
#include<QtGui>
#include<QApplication>
#include<QHBoxLayout>
#include<QSlider>
#include<QSpinBox>
#include"FindDialog.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
FindDialog* pFind = new FindDialog;
pFind->show();
return app.exec();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: