qt qcomboBox实现自动检索功能 通过输入匹配字符进行筛选

本人做了一个自定义控件SeepedSearch 用于快速检索匹配的字符的下拉框 方便查找目标

直接上源码

SpeedSerach.h

#pragma once

#include

class QComboBox;

class QCompleter;

class SpeedSearch : public QWidget

{

Q_OBJECT

public:

explicit SpeedSearch(QWidget *parent = 0);

void initData(const QStringList &strList);

void setEditable(bool bEnabled);

void setCurrentText(const QString& sText);

QString currentText();

void setCurrentIndex(const int& nIdx);

int currentIndex();

void AddItem(const QString& sText);

int Count();

QString itemText(int idx);

void hideSubcontrol();//掩藏下拉框

void setPlaceholderText(QString& sText);

public slots:

void slotCurrentIndexChanged(const QString &str);

signals:

void currentIndexChanged(const QString&);

protected:

void showEvent(QShowEvent *event);

private:

QComboBox *m_comboBox;

QCompleter *m_completer;

};

/

SpeedSearch.cpp

#include "SpeedComboboxSearch.h"

#include
:
QWidget(parent)

复制代码
, m_completer(nullptr)   

{   

m_comboBox = new QComboBox(this);   

m_comboBox-\>setView(new QListView());   

m_comboBox-\>setEditable(true);   

m_comboBox-\>setMaxVisibleItems(30);   

connect(m_comboBox, SIGNAL(activated(QString)), this, SLOT(slotCurrentIndexChanged(QString)));

QVBoxLayout *vLayout = new QVBoxLayout(this);
vLayout->setContentsMargins(0, 0, 0, 0);
vLayout->setSpacing(0);
vLayout->addWidget(m_comboBox);

this->setFixedSize(160, 24);

}

void SpeedSearch::initData(const QStringList &strList)

{

if (m_completer) {

delete m_completer;

}

m_completer = new QCompleter(strList, this);

m_completer->setFilterMode(Qt::MatchContains);

m_comboBox->setCompleter(m_completer);

m_comboBox->clear();

m_comboBox->addItems(strList);

}

void SpeedSearch::setEditable(bool bEnabled)

{

m_comboBox->setEditable(bEnabled);

}

void SpeedSearch::setCurrentText(const QString & sText)

{

m_comboBox->setCurrentText(sText);

}

QString SpeedSearch::currentText()

{

return m_comboBox->currentText();

}

void SpeedSearch::setCurrentIndex(const int & nIdx)

{

m_comboBox->setCurrentIndex(nIdx);

}

int SpeedSearch::currentIndex()

{

return m_comboBox->currentIndex();

}

void SpeedSearch::AddItem(const QString & sText)

{

m_comboBox->addItem(sText);

}

QString SpeedSearch::itemText(int idx)

{

return m_comboBox->itemText(idx);

}

void SpeedSearch::hideSubcontrol()

{

m_comboBox->setStyleSheet("QComboBox::drop-down { subcontrol-origin: padding; width: 0px; height: 0px; image: none; }");

}

void SpeedSearch::setPlaceholderText(QString & sText)

{

m_comboBox->insertItem(0, "");

m_comboBox->setItemText(0, sText);

}

int SpeedSearch::Count()

{

return m_comboBox->count();

}

void SpeedSearch::slotCurrentIndexChanged(const QString &str)

{

qDebug() << str;

//hide();

emit currentIndexChanged(str);

}

void SpeedSearch::showEvent(QShowEvent *event)

{

QWidget::showEvent(event);

// m_comboBox->setCurrentText("");

m_comboBox->setFocus();

}

//注意如果不想用qcombobox 也可以使用qlineEdit替换

这个功能主要关键的一点技术 使用QCompeter

QCompleter *m_completer;

m_completer = new QCompleter(strList, this);

m_completer->setFilterMode(Qt::MatchContains);

m_comboBox->setCompleter(m_completer);

相关推荐
泽02021 分钟前
C++类和对象之相关特性
java·开发语言·c++
敲键盘的小夜猫7 分钟前
深入理解Python逻辑判断、循环与推导式(附实战案例)
开发语言·python
feiyangqingyun29 分钟前
Qt/C++开发监控GB28181系统/录像文件查询/录像回放/倍速播放/录像文件下载
c++·qt·gb28181·录像回放·录像文件下载
为自己_带盐1 小时前
浅聊一下数据库的索引优化
开发语言·数据库·php
明月看潮生1 小时前
青少年编程与数学 02-019 Rust 编程基础 12课题、所有权系统
开发语言·青少年编程·rust·编程与数学
shengjk12 小时前
序列化和反序列化:从理论到实践的全方位指南
java·大数据·开发语言·人工智能·后端·ai编程
passionSnail2 小时前
《用MATLAB玩转游戏开发》推箱子游戏的MATLAB趣味实现
开发语言·游戏·matlab
Once_day2 小时前
C++之fmt库介绍和使用(1)
开发语言·c++·fmt
摆烂且佛系2 小时前
FastByteArrayOutputStream和ByteArrayInputStream有什么区别
java·开发语言
Chandler243 小时前
Go语言:json 作用和语法
开发语言·golang·json