当前位置:网站首页>QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
2022-07-05 23:12:00 【Licht小粉】
示例:
#ifndef ComplexCombox_H
#define ComplexCombox_H
#include <QObject>
#include <QLineEdit>
#include <QEvent>
#include <QKeyEvent>
#include <QWidget>
#include <QDebug>
#include <QTimer>
#include <QComboBox>
#include <QCompleter>
#include <QStringListModel>
#include <QHeaderView>
#include <QLinearGradient>
#include "lightgroupbutton.h"
#include "data/jsondatamanager.h"
#define COMMOM_ADDR_COLOR "#D3D3D3"
#define USED_ADDR_COLOR "#EEEE00"
class ComplexCombox : public QComboBox
{
Q_OBJECT
public:
explicit ComplexCombox(QWidget *parent = 0);
~ComplexCombox();
protected:
//动态加载的话就要重写这个下拉的函数
void showPopup()override;
private:
void initCtr();
void initData();
private:
QComboBox *m_combox;
QCompleter *m_completer;
QList<int> m_qlAddr;
int m_iComboxIndex;
int m_iSelectBtnIndex;
};
#endif // ComplexCombox_H
#include "complexcombox.h"
ComplexCombox::ComplexCombox(QWidget *parent) :
QComboBox(parent)
{
initCtr();
}
ComplexCombox::~ComplexCombox()
{
}
void ComplexCombox::initCtr()
{
m_iComboxIndex=0;
m_iSelectBtnIndex=0;
initData();
setInsertPolicy(QComboBox::InsertAtCurrent);
//setFixedSize(50,20);
setCurrentIndex(0);
setEditable(true);
QRegExp rx("^([A-Da-d]{1}([1-4]{1}[0-9]{2}|[5]{1}[1]{1}[0-2]{1}|[5]{1}[0]{1}[0-9]{1}|[1-9]{1}[0-9]{1}|[1-9]{1}))$");
QRegExpValidator *latitude = new QRegExpValidator(rx,this->lineEdit());
this->lineEdit()->setValidator(latitude);
this->lineEdit()->setCompleter(m_completer);//筛选器要安装在lineEdit里面,不然不会生效
connect(lineEdit(),SIGNAL(editingFinished()),this,SLOT(slotCboxEditFinished()));
connect(this,SIGNAL(activated(QString)),this,SLOT(slotCboxActived(QString)));
}
void ComplexCombox::initData()
{
QStringList slAddr;
for(int i=0;i<2048;i++)
{
m_qlAddr.append(i);
slAddr << playAddrCovertToSaveAddr(i);
}
m_completer = new QCompleter(slAddr,this);
m_completer->setFilterMode(Qt::MatchStartsWith);
m_completer->setCaseSensitivity(Qt::CaseInsensitive);
}
void ComplexCombox::showPopup()
{
emit sigGetCurtBtnIndex(m_iComboxIndex);//没必要,我自己需求需要的
QComboBox::clear();//有必要
QString sCurrentText = this->currentText();//有必要
//这里可以选择你想要下拉框展示的数据
//可以读文件读其他什么的,自己看自己要什么
for(int i=0;i<m_qlAddr.size();i++)
{
addItem(m_qlAddr.value(i));
if(qlTempUsedAddr.contains(m_qlAddr.value(i)))
{
this->setItemData(i,QColor(USED_ADDR_COLOR),Qt::BackgroundColorRole);
//这个可以添加下拉项的背景颜色,也可以根据自己需求来安排
//给下拉框的每一个item添加颜色或者图片可以用setItemData,添加颜色或者图片取决于第三个参数(this就是重载的QComboBox)
}
else
{
this->setItemData(i,QColor(COMMOM_ADDR_COLOR),Qt::BackgroundColorRole);
}
}
setCurrentText(sCurrentText);//有必要
QComboBox::showPopup();//有必要
}
边栏推荐
- yate. conf
- Detailed explanation of pointer and array written test of C language
- Debian 10 installation configuration
- Use of grpc interceptor
- Judge whether the binary tree is a complete binary tree
- idea 连接mysql ,直接贴配置文件的url 比较方便
- 两数之和、三数之和(排序+双指针)
- MySQL replace primary key delete primary key add primary key
- Go语言实现原理——锁实现原理
- Use of shell:for loop
猜你喜欢
Brushless drive design -- on MOS drive circuit
进击的技术er——自动化
数学公式截图识别神器Mathpix无限使用教程
Three. Js-01 getting started
MySQL replace primary key delete primary key add primary key
Practice of concurrent search
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
Week 17 homework
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
Using LNMP to build WordPress sites
随机推荐
UVA11294-Wedding(2-SAT)
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
The maximum happiness of the party
证明 poj 1014 模优化修剪,部分递归 有错误
Douban scoring applet Part-2
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
无刷驱动设计——浅谈MOS驱动电路
进击的技术er——自动化
Shell: operator
poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
Difference between out of band and in band
Using LNMP to build WordPress sites
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
两数之和、三数之和(排序+双指针)
C Primer Plus Chapter 9 question 10 binary conversion
Multi camera stereo calibration
动态规划 之 打家劫舍
Cwaitabletimer timer, used to create timer object access