当前位置:网站首页>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
- 3:第一章:认识JVM规范2:JVM规范,简介;
- Detailed explanation of pointer and array written test of C language
- idea 连接mysql ,直接贴配置文件的url 比较方便
- 698. 划分为k个相等的子集 ●●
- golang代码检查工具
- MySQL (1) -- related concepts, SQL classification, and simple operations
- Krypton Factor purple book chapter 7 violent solution
- What is the process of building a website
- Realize reverse proxy client IP transparent transmission
猜你喜欢
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
Marginal probability and conditional probability
98. Verify the binary search tree ●●
MySQL replace primary key delete primary key add primary key
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Neural structured learning - Part 2: training with natural graphs
Comparison of parameters between TVs tube and zener diode
Pyqt control part (I)
Week 17 homework
Data analysis - Thinking foreshadowing
随机推荐
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
98. 验证二叉搜索树 ●●
Three. JS VR house viewing
Negative sampling
Development specification: interface unified return value format [resend]
Initial experience | purchase and activate typora software
Déterminer si un arbre binaire est un arbre binaire complet
Différence entre hors bande et en bande
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
How to enable relationship view in phpMyAdmin - how to enable relationship view in phpMyAdmin
JVM的简介
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
3:第一章:认识JVM规范2:JVM规范,简介;
(4) UART application design and simulation verification 2 - TX module design (stateless machine)
White hat talks about web security after reading 2
Neural structured learning - Part 3: training with synthesized graphs
Use of grpc interceptor
Dynamic planning: robbing families and houses
Krypton Factor-紫书第七章暴力求解
Practice of concurrent search