QT使用QComBox和QLineEdit實現(xiàn)模糊查詢功能
一、概述
在Qt中,可以通過QComboBox和QLineEdit實現(xiàn)模糊查詢的功能。模糊查詢是指根據(jù)用戶輸入的文本,在下拉框的選項中進(jìn)行模糊匹配,并動態(tài)地顯示匹配的選項。
二、基礎(chǔ)知識
1、QCompleter
(1)QCompleter 是 Qt 框架中提供的一個用于自動補(bǔ)全和模糊搜索的類。QCompleter 可以與輸入框(如 QLineEdit)一起使用,為用戶輸入的文本提供自動補(bǔ)全的功能。
(2)QCompleter 的主要作用是為一個模型(例如 QStringListModel 或 QStandardItemModel)提供自動補(bǔ)全的功能。它可以根據(jù)用戶在輸入框中輸入的文本,動態(tài)地過濾模型中的項,并將過濾結(jié)果作為候選項顯示在輸入框下方的彈出窗口中。
2、QStyledItemDelegate
(1)QStyledItemDelegate 是 Qt 框架中提供的一個用于自定義繪制和編輯每個項的委托類。它可以與諸如 QListView、QTableView、QTreeView 等控件一起使用,用于自定義項的外觀和編輯行為。
(2)QStyledItemDelegate 的主要作用是控制視圖中每個項的繪制和編輯方式,以實現(xiàn)自定義的外觀和交互效果。
三、UI界面搭建

四、具體實現(xiàn)
1、MainWindow.h文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QEvent>
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
//模糊搜索
void obscureSearch();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H2、MainWindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCompleter>
#include <QListView>
#include <QStyledItemDelegate>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
obscureSearch();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::obscureSearch()
{
QStringList list;
list << "RCV_01" << "RCV_02" << "RCV_03" << "RCV_04";
ui->comboBox->addItems(list);
QString style = QString("QListView {"
"font-family: \"Arial\";"
"font-size: 13px; "
"outline: 0px;}"
"QListView::item {"
"padding: 3px 0x 3px 5px;"
"border-width: 0px;}"
"QListView::item:selected {"
"background-color: #004EA2;}"
"QListView::item:hover {"
"background-color: #E5F1FB}");
ui->comboBox->setView(new QListView());
ui->comboBox->setEditable(true);
ui->comboBox->setLineEdit(ui->lineEdit);
ui->comboBox->setMaxVisibleItems(5);
ui->comboBox->view()->setStyleSheet(style);
QCompleter *pCompleter = new QCompleter(list, this);
QStyledItemDelegate *d = new QStyledItemDelegate;
pCompleter->popup()->setItemDelegate(d);
pCompleter->popup()->setStyleSheet(style);
pCompleter->setCaseSensitivity(Qt::CaseInsensitive);
ui->comboBox->setCompleter(pCompleter);
ui->lineEdit->setCompleter(pCompleter);
ui->lineEdit->clear();
}
五、實現(xiàn)效果

到此這篇關(guān)于QT使用QComBox和QLineEdit實現(xiàn)模糊查詢功能的文章就介紹到這了,更多相關(guān)QT模糊查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#?CLR學(xué)習(xí)?C++使用namespace實例詳解
這篇文章主要為大家介紹了C#?CLR學(xué)習(xí)?C++使用namespace實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
C語言數(shù)據(jù)結(jié)構(gòu)與算法之排序總結(jié)(二)
這篇文章住要介紹的是選擇類排序中的簡單、樹形和堆排序,歸并排序、分配類排序的基數(shù)排序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2021-12-12
c 調(diào)用python出現(xiàn)異常的原因分析
本篇文章是對使用c語言調(diào)用python出現(xiàn)異常的原因進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實現(xiàn)LeetCode(11.裝最多水的容器)
這篇文章主要介紹了C++實現(xiàn)LeetCode(11.裝最多水的容器),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
位運算實現(xiàn)十進(jìn)制轉(zhuǎn)換為二進(jìn)制
這篇文章主要介紹了位運算實現(xiàn)十進(jìn)制轉(zhuǎn)換為二進(jìn)制的相關(guān)資料,需要的朋友可以參考下2015-03-03

