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

相关推荐
ChoSeitaku3 分钟前
17.QT-Qt窗口-工具栏|状态栏|浮动窗口|设置停靠位置|设置浮动属性|设置移动属性|拉伸系数|添加控件(C++)
c++·qt·命令模式
Cao12345678932128 分钟前
简易学生成绩管理系统(C语言)
c语言·开发语言
The Future is mine29 分钟前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
亿坊电商32 分钟前
PHP框架在微服务迁移中能发挥什么作用?
开发语言·微服务·php
烁34732 分钟前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法
坐吃山猪1 小时前
Python-Agent调用多个Server-FastAPI版本
开发语言·python·fastapi
88号技师1 小时前
【1区SCI】Fusion entropy融合熵,多尺度,复合多尺度、时移多尺度、层次 + 故障识别、诊断-matlab代码
开发语言·机器学习·matlab·时序分析·故障诊断·信息熵·特征提取
北漂老男孩1 小时前
Java对象转换的多种实现方式
java·开发语言
未来可期LJ1 小时前
【Test】单例模式❗
开发语言·c++
Arenaschi1 小时前
SQLite 是什么?
开发语言·网络·python·网络协议·tcp/ip