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

参考:
Qt 创建一个对话框(上)_w3cschool
https://www.w3cschool.cn/learnroadqt/m4x21j43.html

本地环境:
win10专业版,64位


结果

在这里插入图片描述
在这里插入图片描述

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

文件结构

在这里插入图片描述

代码

main.cpp

#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

#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

#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.Checked2组件没有被选中(默认)
    Qt.PartiallyChecked1组件被半选中
    Qt.Unchecked0组件被选中

    ref:

    PyQt5基本控件详解之QCheckBox(八)_pyqt checkbox-CSDN博客
    https://blog.csdn.net/jia666666/article/details/81533763

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值