qt5-入门-创建文本查找框

参考:

Qt 创建一个对话框(上)_w3cschool

https://www.w3cschool.cn/learnroadqt/m4x21j43.html

本地环境:

win10专业版,64位


结果


当文本框里没有内容时,Find按钮禁用。

文件结构

代码

main.cpp

c 复制代码
#include <QApplication>
#include "finddialog.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    FindDialog *dialog = new FindDialog;
    dialog->show();
    return a.exec();
}

finddialog.h

c 复制代码
#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 *parent = 0);
    ~FindDialog();
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_H

finddialog.cpp

c 复制代码
#include <QtWidgets>
#include "finddialog.h"

FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
    // tr是qt框架提供的函数,用于实现国际化功能,就是将文本字符串翻译成当地语言
    // 需要翻译的字符串应该都使用tr
    // &是表示快捷键,如果按下Alt+W,相当于点击了这个按钮
    label = new QLabel(tr("Find &what"));
    lineEdit = new QLineEdit;
    // 设置为好友,即按下此label指示的快捷键时,键盘焦点转向好友
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));

    // &表示快捷键,如果按下Alt+F,相当于点击了这个按钮
    findButton = new QPushButton(tr("&Find"));
    // default属性指对话框中有enter键按下时执行此按键的click()事件,默认是false,这里表示开启
    findButton->setDefault(true);
    // 默认禁用
    findButton->setEnabled(false);

    closeButton = new QPushButton(tr("Close"));

    // 当lineEdit内容发生改变时,enableFindButton()被调用
    connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&)));
    // 当点击findButton时,findClicked()被调用
    connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
    // 关闭窗口
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

    // 布局
    // 左上角第一行放label和输入框
    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);
    // 左栏,上面是第一行的,下面是俩checkbox
    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);
    // 设置当前控件的布局
    this->setLayout(mainLayout);
    this->setWindowTitle(tr("Find"));
    // sizeHint()是一个推荐尺寸
    this->setFixedHeight(sizeHint().height());
}

FindDialog::~FindDialog(){}

void FindDialog::findClicked(){
    QString text = lineEdit->text();
    // 复选框Qt.Checked表示组件没有被选中,是默认情况
    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
    // 如果没有选中向后查找,那么就是向前
    if(backwardCheckBox->isChecked()) {
        emit findPrevious(text, cs);
    } else {
        emit findNext(text, cs);
    }
}

void FindDialog::enableFindButton(const QString &text){
    // 只有text不为空时才能启用find按钮
    findButton->setEnabled(!text.isEmpty());
}

概念解释

  1. Qt中报错error: allocation of incomplete type QLabel

    #include <QtGui> 改成 #include <QtWidgets> 即可。这是qt4到qt5升级所致。

    ref: Qt中报错error: allocation of incomplete type QLabel_allocation of incomplete type 'qdrag-CSDN博客

    最好不要直接导入QtWidgets,应该是用到什么导入什么

  2. Qt中的tr函数是什么?

    tr是qt框架提供的函数,用于实现国际化功能,就是将文本字符串翻译成当地语言。需要自动翻译的字符串应该都放入tr中。

    tr()是定义在Object中的,所以只要使用了Q_OBJECT宏的类都自动拥有tr()。

    ref: https://blog.csdn.net/m0_61629312/article/details/134454370

  3. 好友机制

    当用户按下此label指示的快捷键时,键盘焦点将转移到label的好友小部件。好友机制适用于包含一个字符前缀为"&"的文本的QLabels。

    &表示快捷键

    ref: https://blog.csdn.net/hellokandy/article/details/130345047

  4. 按钮的default属性

    指对话框中有enter按下时,当前button会自动执行click事件,默认是false

    QPushButton的default和autoDefault 属性_qpushbutton autodefault-CSDN博客

    https://blog.csdn.net/yaowangII/article/details/84580597

  5. Qt.Checked表示没有被选中

    名称 含义
    Qt.Checked 2 组件没有被选中(默认)
    Qt.PartiallyChecked 1 组件被半选中
    Qt.Unchecked 0 组件被选中

    ref:

    PyQt5基本控件详解之QCheckBox(八)_pyqt checkbox-CSDN博客

    https://blog.csdn.net/jia666666/article/details/81533763

  6. Qt::CaseSensitivity 为枚举类型, 可取值Qt::CaseSensitive 和 Qt::CaseInsensitive

相关推荐
用户805533698033 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner3 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz8 天前
QML Hello World 入门示例
qt
xcyxiner11 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner11 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner12 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00614 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术14 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript