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

相关推荐
三体世界4 分钟前
TCP传输控制层协议深入理解
linux·服务器·开发语言·网络·c++·网络协议·tcp/ip
随心点儿25 分钟前
使用python 将多个docx文件合并为一个word
开发语言·python·多个word合并为一个
不学无术の码农29 分钟前
《Effective Python》第十三章 测试与调试——使用 Mock 测试具有复杂依赖的代码
开发语言·python
tomcsdn3135 分钟前
SMTPman,smtp的端口号是多少全面解析配置
服务器·开发语言·php·smtp·邮件营销·域名邮箱·邮件服务器
EnigmaCoder39 分钟前
Java多线程:核心技术与实战指南
java·开发语言
大白的编程日记.2 小时前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习
网小鱼的学习笔记2 小时前
python中MongoDB操作实践:查询文档、批量插入文档、更新文档、删除文档
开发语言·python·mongodb
Q_Q5110082853 小时前
python的保险业务管理与数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
亮1113 小时前
Maven 编译过程中发生了 Java Heap Space 内存溢出(OutOfMemoryError)
java·开发语言·maven
Chef_Chen3 小时前
从0开始学习R语言--Day40--Kruskal-Wallis检验
开发语言·学习·r语言