《C++ GUI Qt4编程》第2章——创建对话框——子类化QDialog
2.1 子类化QDialog
示例 Find
"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();
}
"finddialog.h"
#ifndef FINDDIALOG_H // 能够防止对这个头文件的多重包含
#define FINDDIALOG_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
#include <QHBoxLayout>
class FindDialog : public QDialog
{
// 对于所有定义了信号与槽的类,在类定义开始处的 Q_OBJECT宏都是必需的。
Q_OBJECT
public:
// parent参数指定了它的父窗口部件。该参数的默认值是一个空指针,意味着该对话框没有父对象。
FindDialog(QWidget *parent = 0);
signals:
// signals部分声明了当用户单击Find按钮时对话框所发射的两个信号。
// 如果向前查询(search backward)选项生效,对话框就发射findPrevious()信号,否则它就发射findNext()信号。
// signals关键字实际上是一个宏。C++预处理器会在编译程序找到它之前把它转换成标准C++代码。
// Qt::CaseSensitivity是一个枚举类型,它有Qt::CaseSensitive和Qt::CaseInsensitive两个取值。
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.cpp"
#include <QtGui>
#include "finddialog.h"
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
// 字符串周围的tr()函数调用是把它们翻译成其他语言的标记。
// "&"符号表示快捷键,下面可以用(Alt+W)激活
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
// 伙伴“Buddy”,在按下快捷键时可以接收焦点
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
// "&"符号表示快捷键,下面可以用(Alt+F)激活
findButton = new QPushButton(tr("&Find"));
// Default按钮就是按下Enter键时对应的按钮
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 &text)
{
findButton->setEnabled(!text.isEmpty());
}
分类:
Qt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!