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

相关推荐
归寻太乙几秒前
C++函数重载完成日期类相关计算
开发语言·c++
尽蝶叙3 分钟前
C++:分苹果【排列组合】
开发语言·c++·算法
憧憬成为原神糕手28 分钟前
c++_list
开发语言·c++
idealzouhu30 分钟前
Java 并发编程 —— AQS 抽象队列同步器
java·开发语言
爱吃油淋鸡的莫何31 分钟前
Conda新建python虚拟环境问题
开发语言·python·conda
闲人编程38 分钟前
Python实现日志采集功能
开发语言·python·fluentd·filebeat·日志采集
Sol-itude44 分钟前
关于MATLAB计算3维图的向量夹角总是不正确的问题记录
开发语言·matlab·问题解决·向量
奔驰的小野码1 小时前
java通过org.eclipse.milo实现OPCUA客户端进行连接和订阅
java·开发语言
小川_wenxun1 小时前
优先级队列(堆)
java·开发语言·算法
惜缘若水1 小时前
【SpinalHDL】Scala编程之伴生对象
开发语言·scala·spinalhdl