【Qt编程】QLineEdit自动补齐QCompleter的用法

【Qt编程】QLineEdit自动补齐QCompleter的用法

版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.youkuaiyun.com/weixin_40426136/article/details/90904513

第一次写博客,话说,这是博客吗?就当个笔记吧,最近在做一个项目,其中想在软件中实现一个搜索补全的功能,网上搜了很多,发现。QLineEdit有个自动补齐QCompleter。于是决定用它来实现。废话不多说,上代码。

在这里插入图片描述

代码部分

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qcompleter_test.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
public:
    QCompleter_test *m_widget;

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_widget = new QCompleter_test;
    m_widget->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}


qcompleter_test.h

#ifndef QCOMPLETER_TEST_H
#define QCOMPLETER_TEST_H

#include <QDialog>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QCompleter>
#include <QStandardItemModel>
#include <QLabel>

namespace Ui {
class QCompleter_test;
}

class QCompleter_test : public QDialog
{
    Q_OBJECT

public:
    explicit QCompleter_test(QWidget *parent = nullptr);
    ~QCompleter_test();

public:
     QHBoxLayout *m_qhboxlayout;
     QVBoxLayout *m_qvboxlayout;
     QPushButton *m_button;
     QLineEdit *m_lineEdit;
     QStandardItemModel *m_model;
     QCompleter *m_completer;
     QLabel *m_label;


public:
     void widget_show();
public slots:
     void Search_Button_Down();
     void LineEdit_Text_Clean(const QString&);
     void LineEdit_Text_Changed(const QString &text);

private:
    Ui::QCompleter_test *ui;
};

#endif // QCOMPLETER_TEST_H

qcompleter_test.cpp

/******************************
 * time:2019.6.5
 * name:ZStinkie
 * QCompleter 可以设置三种匹配模式,分别为 开头匹配(默认);内容匹配;末尾匹配
 * 设置方法如下
 * m_completer->setFilterMode(Qt::MatchStartsWith);//开头匹配(默认)
 * m_completer->setFilterMode(Qt::MatchContains);//内容匹配
 * m_completer->setFilterMode(Qt::MatchEndsWith);//末尾匹配
 *
 * 个人心得:感觉一个(Qt::MatchContains);//内容匹配   就够用了。
 * 只要是包含这个字或者词,不管是开头,中间还是结尾都可以联想匹配到
 * ***************************/

#include "qcompleter_test.h"
#include "ui_qcompleter_test.h"

QCompleter_test::QCompleter_test(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QCompleter_test)
{
    ui->setupUi(this);

    widget_show();

    connect(m_button,SIGNAL(clicked()),this,SLOT(Search_Button_Down()));
    connect(m_completer, SIGNAL(activated(const QString&)), this, SLOT(LineEdit_Text_Clean(const QString&)));
    connect(m_lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(LineEdit_Text_Changed(const QString&)));

}

void QCompleter_test::widget_show()
{
    m_qvboxlayout = new QVBoxLayout;
    m_qhboxlayout = new QHBoxLayout;

    m_button = new QPushButton("搜索");
    m_lineEdit = new QLineEdit;
    m_model = new QStandardItemModel(0, 1, this);
    m_completer = new QCompleter(m_model, this);
    m_label = new QLabel;

    m_qhboxlayout->addWidget(m_lineEdit);
    m_qhboxlayout->addWidget(m_button);

    m_lineEdit->setClearButtonEnabled(true);
    m_lineEdit->installEventFilter(this);

    m_lineEdit->setPlaceholderText(QStringLiteral("请输入搜索内容"));
    m_completer->setFilterMode(Qt::MatchContains);//将匹配方式设置为 内容匹配
    //注:这里如果不设置默认为开头匹配。
    m_completer->setCaseSensitivity(Qt::CaseInsensitive);//忽略大小写
    m_lineEdit->setCompleter(m_completer);

    //m_qvboxlayout->addStretch();
    m_qvboxlayout->addLayout(m_qhboxlayout);
    m_qvboxlayout->addWidget(m_label);
    //m_qvboxlayout->addStretch();

    setWindowTitle(tr("QCompleter"));
    setLayout(m_qvboxlayout);
}

void QCompleter_test::Search_Button_Down()
{
    QString strText = m_lineEdit->text();
    m_label->setText(tr("搜索的内容为:%1").arg(strText));
}

void QCompleter_test::LineEdit_Text_Clean(const QString &email)
{
    m_lineEdit->clear();
    m_lineEdit->setText(email);
}

void QCompleter_test::LineEdit_Text_Changed(const QString &text)
{
    QStringList word_strlist;

    //word_strlist<<"Qt"<<"C++"<<"C"<<"JAVA"<<"C#"<<"PHP"<<"Python";
    word_strlist<<"你好"<<"好的"<<"好吗"<<"你的"<<"真好啊"<<"天真"<<"你好吗"<<"你我它"<<"你是谁"<<"你们";

    m_model->removeRows(0, m_model->rowCount());
    for (int i = 0; i < word_strlist.size(); ++i)
    {
        m_model->insertRow(0);
        m_model->setData(m_model->index(0, 0),word_strlist.at(i));
    }
}

QCompleter_test::~QCompleter_test()
{
    delete ui;
}

软件说明

没有基于MainWindow实现,而是通过MainWindow调用Qcompleter窗口
在这里插入图片描述

这里直接将MainWindow窗口关闭即可,输入首字,成功匹配。
在这里插入图片描述
输入句中,亦可以成功匹配
在这里插入图片描述
大致就是这样
在这里插入图片描述

特别说明

本文参考了以下大神的例子
https://blog.youkuaiyun.com/tengweitw/article/details/38689745

http://www.imooc.com/article/74617

http://www.cppblog.com/biao/archive/2009/10/31/99873.html

https://www.cnblogs.com/lanye/p/3533786.html

最后

第一次发布,请各位大神批评指导,也希望我的这篇文章能帮到那些需要帮助的人。
代码下载地址 https://download.youkuaiyun.com/download/weixin_40426136/11227357
2019.6.5 ZStinkie

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值