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);

相关推荐
Knight_AL23 分钟前
浅拷贝与深拷贝详解:概念、代码示例与后端应用场景
android·java·开发语言
枫叶丹425 分钟前
【Qt开发】输入类控件(六)-> QDial
开发语言·qt
思考的笛卡尔1 小时前
Go语言实战:高并发服务器设计与实现
服务器·开发语言·golang
努力努力再努力wz1 小时前
【C++进阶系列】:万字详解智能指针(附模拟实现的源码)
java·linux·c语言·开发语言·数据结构·c++·python
凤年徐1 小时前
【C++】string的模拟实现
c语言·开发语言·c++
敲代码的嘎仔1 小时前
JavaWeb零基础学习Day2——JS & Vue
java·开发语言·前端·javascript·数据结构·学习·算法
吃鱼吃鱼吃不动了1 小时前
什么是负载均衡?
开发语言·php
小蕾Java2 小时前
Python详细安装教程(附PyCharm使用)
开发语言·python·pycharm
weixin_307779132 小时前
AWS云上ClickHouse数据仓库部署方案详解
开发语言·clickhouse·自动化·云计算·aws
weixin_307779132 小时前
使用AWS IAM和Python自动化权限策略分析与导出
开发语言·python·自动化·云计算·aws