当前位置: 首页 > news >正文

Qt开发技术【下拉复选框 MultiSelectComboBox 自定义全选项】

继承ComboBox完成下拉复选框

自定义全选项
效果图
在这里插入图片描述
在这里插入图片描述

整个控件继承于QCombobox类。主要修改QLineEdit、QListWidget这两部分,QComboBox提供如下接口,可以将这两部分设置为新建的QLineEdit、QListWidget对象

CMultiSelectComboBox::CMultiSelectComboBox(QWidget* parent, bool bHasAllSelected): QComboBox(parent), m_bHasAllSelected(bHasAllSelected)
{m_pListWidget = new QListWidget();m_pLineEdit = new QLineEdit();m_pLineEdit->setReadOnly(true);setModel(m_pListWidget->model());setView(m_pListWidget);setLineEdit(m_pLineEdit);m_pLineEdit->installEventFilter(this);if (m_bHasAllSelected){AddItem(TransString2Unicode("全选"), ALL_SELECTED);}
}

完整代码

/*
**  File name:   MultiSelectComboBox.h
**  Author:      
**  Date:        2024-12-26
**  Brief:       多选下拉框控件
**  Copyright (C) 1392019713@qq.com All rights reserved.
*/#pragma once#include <QComboBox>
#include <QListWidget>
#include <QLineEdit>
#include <QVariant>
#include <QEvent>
#include "QtGuiExportLib.h"class QTGUI_EXPORT CMultiSelectComboBox : public QComboBox
{
public:/** @brief 构造函数* @param parent 父控件* @param bHasAllSelected 是否显示全选选项*/explicit CMultiSelectComboBox(QWidget* parent = nullptr, bool bHasAllSelected = false);virtual ~CMultiSelectComboBox() = default;/** @brief 设置是否显示全选选项 需在设置数据前调用* @param bHasAllSelected 是否显示全选选项*/void SetHasAllSelected(bool bHasAllSelected = true);void AddItem(const QString& qstrItem, const QVariant& rVar = QVariant());void RemoveItem(const QVariant& rVar);void ClearItems();QList<QVariant> GetSelectedItemDatas() const;QString GetCurrentText() const;protected:virtual bool eventFilter(QObject* pObj, QEvent* pEvent) override;private:void SetSelectedCheckState(int nState);void SetAllSelected(bool bChecked);int GetCheckedCount() const;private Q_SLOTS:void SlotItemStateChanged(int);void SlotItemClicked(int nIndex);void SlotChecBoxClicked();private:bool m_bHasAllSelected;QListWidget* m_pListWidget;QLineEdit* m_pLineEdit;
};
#include "../Include/MultiSelectComboBox.h"
#include "../Include/Conversion.h"
#include <QCheckBox>
#include <QDebug>#define ALL_SELECTED  "ALL_SELECT" CMultiSelectComboBox::CMultiSelectComboBox(QWidget* parent, bool bHasAllSelected): QComboBox(parent), m_bHasAllSelected(bHasAllSelected)
{m_pListWidget = new QListWidget();m_pLineEdit = new QLineEdit();m_pLineEdit->setReadOnly(true);setModel(m_pListWidget->model());setView(m_pListWidget);setLineEdit(m_pLineEdit);m_pLineEdit->installEventFilter(this);if (m_bHasAllSelected){AddItem(TransString2Unicode("全选"), ALL_SELECTED);}
}void CMultiSelectComboBox::SetHasAllSelected(bool bHasAllSelected)
{if (bHasAllSelected){AddItem(TransString2Unicode("全选"), ALL_SELECTED);}else{RemoveItem(ALL_SELECTED);}m_bHasAllSelected = bHasAllSelected;
}void CMultiSelectComboBox::AddItem(const QString& qstrItem, const QVariant& rVar)
{QListWidgetItem* pItem = new QListWidgetItem(m_pListWidget);QCheckBox* pCheckBox = new QCheckBox();pCheckBox->setText(qstrItem);pItem->setData(Qt::UserRole, rVar);m_pListWidget->addItem(pItem);m_pListWidget->setItemWidget(pItem, pCheckBox);connect(pCheckBox, &QCheckBox::stateChanged, this, &CMultiSelectComboBox::SlotItemStateChanged);connect(pCheckBox, &QCheckBox::clicked, this, &CMultiSelectComboBox::SlotChecBoxClicked);
}void CMultiSelectComboBox::RemoveItem(const QVariant& rVar)
{for (int i = 0; i < m_pListWidget->count(); i++){QListWidgetItem* pItem = m_pListWidget->item(i);if (!pItem){continue;}if (pItem->data(Qt::UserRole) == rVar){QWidget* pWidget = m_pListWidget->itemWidget(pItem);QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(pWidget);if (pCheckBox){delete pCheckBox;}m_pListWidget->removeItemWidget(pItem);m_pListWidget->takeItem(i);delete pItem;break;}}
}void CMultiSelectComboBox::ClearItems()
{m_pListWidget->clear();m_pLineEdit->clear();
}QList<QVariant> CMultiSelectComboBox::GetSelectedItemDatas() const
{QList<QVariant> lstVars;int i = 0;if (m_bHasAllSelected){i = 1;}for (; i < m_pListWidget->count(); i++){QListWidgetItem* pItem = m_pListWidget->item(i);if (!pItem){continue;}QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(m_pListWidget->itemWidget(pItem));if (!pCheckBox){continue;}if (pCheckBox->isChecked()){QVariant rVar = pItem->data(Qt::UserRole);if (rVar.isValid()){lstVars.append(rVar);}}}return lstVars;
}QString CMultiSelectComboBox::GetCurrentText() const
{if (!m_pLineEdit){return QString();}return  m_pLineEdit->text();
}bool CMultiSelectComboBox::eventFilter(QObject* pObj, QEvent* pEvent)
{if (pObj == m_pLineEdit){if (pEvent->type() == QEvent::MouseButtonPress){showPopup();return true;}}return false;
}void CMultiSelectComboBox::SetSelectedCheckState(int nState)
{for (int i = 0; i < m_pListWidget->count(); i++){QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(m_pListWidget->itemWidget(m_pListWidget->item(i)));if (!pCheckBox){continue;}if (pCheckBox->isChecked()){pCheckBox->setCheckState(Qt::CheckState(nState));}}
}void CMultiSelectComboBox::SetAllSelected(bool bChecked)
{for (int i = 0; i < m_pListWidget->count(); i++){QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(m_pListWidget->itemWidget(m_pListWidget->item(i)));if (!pCheckBox){continue;}pCheckBox->setChecked(bChecked);}
}int CMultiSelectComboBox::GetCheckedCount() const
{int i = 0;if (m_bHasAllSelected){i = 1;}int nCount = 0;for (; i < m_pListWidget->count(); i++){QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(m_pListWidget->itemWidget(m_pListWidget->item(i)));if (!pCheckBox){continue;}if (pCheckBox->isChecked()){nCount++;}}return nCount;
}void CMultiSelectComboBox::SlotItemClicked(int nIndex)
{QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(m_pListWidget->itemWidget(m_pListWidget->item(nIndex)));if (!pCheckBox){return;}if (m_bHasAllSelected && nIndex == 0){SetAllSelected(pCheckBox->isChecked());}else if(!m_bHasAllSelected){pCheckBox->setChecked(pCheckBox->isChecked());}else {pCheckBox->setChecked(pCheckBox->isChecked());int nCheckedCount = GetCheckedCount();if (nCheckedCount == 0){SetAllSelected(false);}else if (nCheckedCount < m_pListWidget->count() - 1){SetSelectedCheckState(Qt::PartiallyChecked);}else{SetAllSelected(true);}}
}void CMultiSelectComboBox::SlotChecBoxClicked()
{int nIndex = 0;QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(sender());if (!pCheckBox){return;}for (int i = 0; i < m_pListWidget->count(); i++){if (m_pListWidget->itemWidget(m_pListWidget->item(i)) == pCheckBox){nIndex = i;break;}}SlotItemClicked(nIndex);
}void CMultiSelectComboBox::SlotItemStateChanged(int nState)
{QString qstrText;int i = 0;if (m_bHasAllSelected){i = 1;}for (; i < m_pListWidget->count(); i++){QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(m_pListWidget->itemWidget(m_pListWidget->item(i)));if(!pCheckBox){continue;}if (pCheckBox->isChecked()){qstrText += pCheckBox->text() + TransString2Unicode("、");}}qstrText.chop(1);m_pLineEdit->setText(qstrText);m_pLineEdit->setToolTip(qstrText);
}

使用示例 可以提升控件也可以直接new

提升
在这里插入图片描述

    ui->comboBox->SetHasAllSelected(true);ui->comboBox->AddItem("Sine Wave", 1);ui->comboBox->AddItem("Random Data", 2);ui->comboBox->AddItem("Custom Data", 3);ui->comboBox->AddItem("Custom Data 2", 4);ui->comboBox->RemoveItem(1);

如果此文帮助到你,动动小手点个赞可好在这里插入图片描述

http://www.lryc.cn/news/510031.html

相关文章:

  • 20_HTML5 SSE --[HTML5 API 学习之旅]
  • jetson Orin nx + yolov8 TensorRT 加速量化 环境配置
  • Android Studio IDE环境配置
  • PTA 7-2 0/1背包问题(回溯法) 作者 王东 单位 贵州师范学院
  • Matlab环形柱状图
  • 【AI大模型】探索GPT模型的奥秘:引领自然语言处理的新纪元
  • 5.Python爬虫相关
  • Windows系统上配置eNSP环境的详细步骤
  • Database.NET——一款轻量级多数据库客户端工具
  • 新浪微博C++面试题及参考答案
  • 计算机视觉目标检测-1
  • 【物联网技术与应用】实验15:电位器传感器实验
  • java常用类(上)
  • 包管理工具npm、yarn、pnpm、cnpm详解
  • CI/CD是什么?
  • [Java]合理封装第三方工具包(附视频)
  • 常规配置、整合IDEA
  • 用Python写炸金花游戏
  • 计算机的错误计算(一百九十二)
  • 37 Opencv SIFT 特征检测
  • Nginx界的天花板-Oracle 中间件OHS 11g服务器环境搭建
  • 域名解析协议
  • 微信小程序给外面的view设置display:flex;后为什么无法给里面的view设置宽度
  • Maven怎么会出现一个dependency-reduced-pom.xml的文件
  • 突发!!!GitLab停止为中国大陆、港澳地区提供服务,60天内需迁移账号否则将被删除
  • 自学记录HarmonyOS Next DRM API 13:构建安全的数字内容保护系统
  • Vue 3 + Element Plus 实现文件上传组件:详细解析与实现指南
  • qt5.12.11+msvc编译器编译qoci驱动
  • Ubuntu 20.04 安装 LNMP
  • Llama 3 简介(一)