一、查找对话框UI
代码如下(示例):
#ifndef FINDDIALONG_H
#define FINDDIALONG_H
#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDIalong:public QDialog
{
Q_OBJECT
public:
explicit FindDIalong(QWidget *parent = 0);
signals:
void signalFindNext(const QString &str,Qt::CaseSensitivity);
void signalFindPre(const QString &str,Qt::CaseSensitivity);
private slots:
void slotFindClick();
void slotEnableFindButton(const QString &text);
private:
QLabel *mPtr_label;
QLineEdit *mPtr_lineEdit;
QCheckBox *mPtr_caseCheckBox;
QCheckBox *mPtr_backwrdCheckBox;
QPushButton *mPtr_findButton;
QPushButton *mPtr_closeButton;
};
#endif // FINDDIALONG_H
代码如下(示例):
#include "find_dialong.h"
#include <QtGui>
FindDIalong::FindDIalong(QWidget *parent):QDialog(parent)
{
//&表示快捷键,也可以控制焦点
mPtr_label = new QLabel(tr("Find &what:"));
mPtr_lineEdit = new QLineEdit;
//添加伙伴,当mPtr_label按下快捷键的时候,接受焦点至mPtr_lineEdit
mPtr_label->setBuddy(mPtr_lineEdit);
mPtr_caseCheckBox = new QCheckBox(tr("Match &case"));
mPtr_backwrdCheckBox = new QCheckBox(tr("Search &backward"));
mPtr_findButton = new QPushButton("&Find");
mPtr_findButton->setDefault(true);
mPtr_findButton->setEnabled(false);
mPtr_closeButton = new QPushButton(tr("close"));
connect(mPtr_lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(slotEnableFindButton(const QString&)));
connect(mPtr_findButton,SIGNAL(clicked()),this,SLOT(slotFindClick()));
connect(mPtr_closeButton,SIGNAL(clicked()),this,SLOT(close()));
QHBoxLayout *topLeftLayout =new QHBoxLayout;
topLeftLayout->addWidget(mPtr_label);
topLeftLayout->addWidget(mPtr_lineEdit);
QVBoxLayout *leftLayout = new QVBoxLayout;
leftLayout->addWidget(mPtr_caseCheckBox);
leftLayout->addWidget(mPtr_backwrdCheckBox);
leftLayout->addLayout(topLeftLayout);
QVBoxLayout *reftLayout = new QVBoxLayout;
reftLayout->addWidget(mPtr_findButton);
reftLayout->addWidget(mPtr_closeButton);
///添加分隔符,即小弹簧
reftLayout->addStretch();
QHBoxLayout *mainLayout =new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(reftLayout);
setLayout(mainLayout);
///sizeHint().height() 自动生成一个理想大小的尺寸
setFixedHeight(sizeHint().height());
}
void FindDIalong::slotFindClick()
{
QString text = mPtr_lineEdit->text();
///Qt::CaseSensitivity 大小写敏感
Qt::CaseSensitivity cs = mPtr_caseCheckBox->isChecked() ? Qt::CaseSensitive:Qt::CaseInsensitive;
if(mPtr_backwrdCheckBox->isChecked())
{
emit signalFindNext(text,cs);
}
else
{
emit signalFindPre(text,cs);
}
}
void FindDIalong::slotEnableFindButton(const QString &text)
{
mPtr_findButton->setEnabled(!text.isEmpty());
}