当前位置:网站首页>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();//有必要
}
边栏推荐
猜你喜欢
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
Data analysis - Thinking foreshadowing
Using LNMP to build WordPress sites
Basic knowledge of database (interview)
Three. Js-01 getting started
698. 划分为k个相等的子集 ●●
Sum of two numbers, sum of three numbers (sort + double pointer)
Douban scoring applet Part-2
动态规划 之 打家劫舍
Week 17 homework
随机推荐
Go语言实现原理——Map实现原理
Calculating the number of daffodils in C language
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
数据库基础知识(面试)
Use of shell:for loop
TVS管和ESD管的技術指標和選型指南-嘉立創推薦
开关电源Buck电路CCM及DCM工作模式
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
Data analysis - Thinking foreshadowing
6-axis and 9-axis IMU attitude estimation
Douban scoring applet Part-2
Difference between out of band and in band
Introduction to JVM
February 13, 2022-4-symmetric binary tree
Basic knowledge of database (interview)
LeetCode——Add Binary
CJ mccullem autograph: to dear Portland
698. 划分为k个相等的子集 ●●
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial