FindDialog.pro
######################################################################
# Automatically generated by qmake (3.1) Wed Dec 22 19:24:23 2021
######################################################################
QT += core gui
QT += widgets
#如果不加上面这句,那么在头文件中所有的对象类的引入都要改为#include<QtWidgets/类名>
TEMPLATE = app
TARGET = FindDialog
INCLUDEPATH += .
# You can make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# Please consult the documentation of the deprecated API in order to know
# how to port your code away from it.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
# Input
HEADERS += finddialog.h
SOURCES += finddialog.cpp main.cpp
finddialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QtWidgets/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 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 "finddialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QPushButton>
FindDialog::FindDialog(QWidget* parent)
:QDialog(parent)
{
label = new QLabel(tr("Find &what:"));//&表示快捷键Alt+w
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);//设置成伙伴,就是一个窗口部件,当按下快捷键时能接受焦点
//例如本代码中,客户按下Alt+w,本来激活的是label,但是焦点会落在lineEdit
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);//把find按钮设置成对话框的默认按钮,当客户按下enter键时,默认执行本按钮
findButton->setEnabled(false);//find按钮设置成启动禁用
closeButton = new QPushButton(tr("Close"));
//QObject是FindDialog的父对象之一,所以可以省略connect()函数前面的QObject::前缀
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();//添加弹簧addStretch()
//继承自void QBoxLayout::addStretch(int stretch = 0)
//水平布局就是水平弹簧,垂直布局就是垂直弹簧
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
//布局管理器不是窗口部件,派生自QLayout,进一步派生自QObject
//当子布局对象添加到父布局对象中时,子布局对象会重新定义自己的父对象
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;
backwardCheckBox->isChecked()? emit findPrevious(text,cs) : 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();
}