- 在每个QObjet对象以及包含有Q_OBJECT宏的子类中都有tr()函数的声明。
- 伙伴(buddy)是一个窗口部件,可以在按下标签的快捷键时接收焦点,焦点就会移动到该标签的伙伴上。
编写一个Find查找对话框
TEMPLATE = app
HEADERS = finddialog.h
SOURCES = finddialog.cpp \
main.cpp
QT += core gui widgets
- finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog> ///Qt对话框的基类,从QWidget类中派生出来
#include <QtWidgets> ///Qt5需要
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT ///定义了信号和槽的类,类定义开始处此宏必须
public: ///parent参数指定它的父窗口部件,默认值为空指针,即没有父对象
FindDialog(QWidget *parent = nullptr); ///nullptr代替0剔除警告,
signals: ///宏,点击Find按钮时对话框所发射的两个信号
//枚举类型:Qt::CaseSensitivity 区分大小写 Qt::CaseInsensitive 不区分大小写
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots: ///宏。为了实现这2个槽,几乎需要访问这个对话框的所有子窗口部件
///保留了指向它们的指针
void findClicked();
void enableFindButton(const QString &text);
private: ///前置声明,使编译过程更快
QLabel *label;
QLineEdit *lineEdit;
QCheckBox *caseCheckBox;
QCheckBox *backwardCheckBox;
QPushButton *findButton;
QPushButton *closeButton;
};
#endif
- finddialog.cpp
#include <QtGui> //Qt由数个模块构成,头文件为构成QtCore,QtGui组成部分的所有类进行了定义
#include "finddialog.h"
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent) ///创建子窗口部件
{ ///在每一个用户可见的字符串周围使用tr()是一个很不错的习惯
label = new QLabel(tr("Find &what:"));///tr()把它们翻译成其它语言标记,快捷键Alt+W
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);///设置行编辑器作为标签的伙伴(buddy)
///“与”操作符“&”来表示快捷键,可以控制焦点
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));///快捷键Alt+F
findButton->setDefault(true);///让Find按钮成为对话框的默认按钮
findButton->setEnabled(false);///禁用Find按钮
closeButton = new QPushButton(tr("Close"));
///省略connect前面的QObject::,因为QObject是FindDialog的父对象之一
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());
///QWidget::sizeHint()可以返回一个窗口部件的“理想”尺寸大小
}
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 &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();
}
- 运行