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

无锡网站App微信南京企业网站排名优化

无锡网站App微信,南京企业网站排名优化,推广普通话喜迎二十手抄报,erp软件操作流程继承ComboBox完成下拉复选框 自定义全选项 效果图 整个控件继承于QCombobox类。主要修改QLineEdit、QListWidget这两部分,QComboBox提供如下接口,可以将这两部分设置为新建的QLineEdit、QListWidget对象 CMultiSelectComboBox::CMultiSelectComboBo…

继承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.yidumall.com/news/10184.html

相关文章:

  • 南京做网站yuanmusjsurl转码
  • 服装网页设计网站网站推广的方式和方法
  • 贵州国龙翔建设有限公司网站英文谷歌seo
  • 西安医疗网站制作湖北荆门今日头条
  • 全国十大室内设计师seo有些什么关键词
  • 网站的新闻模块怎么做建设网站的网络公司
  • 301转向 所有网站一起操作学seo如何入门
  • 广州找工作哪个网站好windows优化软件排行
  • 建网站怎么做网络推广seo怎么弄
  • 建企业网站浩森宇特如何开通自己的网站
  • 淘宝上的网站建设网络搜索引擎有哪些
  • 网站建设技术网网站优化公司推荐
  • 网站建设中+网页代码网站代运营推广
  • 公司网站如何做维护360搜索建站
  • 34线城市做网站推广二级域名网站查询入口
  • 台州那家网站做的好怎么做网站赚钱
  • 网站做301跳转的好处seo搜索引擎优化课程总结
  • 怎么注册一个自己的平台seo工作
  • 三元爱力优网站建设日期关键词优化包含
  • 天津做网站多少钱国际最新消息
  • 中国优秀网站设计百度推广首次开户需要多少钱
  • 4网站免费建站头条权重查询站长工具
  • 国外品牌网站海外新闻发布
  • 从化网站建设公司竞价推广开户多少钱
  • 吉林做网站公司广告软文范例大全100
  • 陕西省交通建设集团公司招聘网站新网站如何快速收录
  • 做网站适合用什么字体微信公众号推广网站
  • 网站是如何建设的搜索引擎营销方法有哪些
  • 网络工程建设流程seo营销软件
  • 如何在相关网站免费做宣传广告关键词网站排名查询