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

群辉做网站服务器配置上海seo推广方法

群辉做网站服务器配置,上海seo推广方法,长春招聘网智联,平面设计去哪里找工作继承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.hkea.cn/news/579501/

相关文章:

  • 常州个人做网站制作小程序的软件
  • 郑州做网站公司dz论坛如何seo
  • 十堰商城网站建设网络营销seo优化
  • 小欢喜林磊儿什么网站做家教福州seo推广外包
  • 许昌网站开发博客营销
  • 做网站用jquery爱站网关键词挖掘
  • wordpress手动裁剪seo营销推广服务公司
  • 英文网站建设网站海南网站制作公司
  • 网页设计与网站建设主要内容软文营销的特点
  • 一起做网站17广州最新小组排名
  • 最专业的网站设计公司有哪些论坛企业推广
  • 单页网站怎么做外链个人网页
  • 宁波城乡住房建设局网站有效的网络推广
  • 网站建设 深圳销售crm客户管理系统
  • 高端网站开发设计站长之家字体
  • 免费网站建站工具购买域名的网站
  • 淘宝联盟怎么做网站百度网站提交
  • 前端做用vue做后台多还是做网站多青岛网站快速排名优化
  • 岳阳网站开发公司海淀区seo多少钱
  • 2017年做网站维护总结百度搜索软件
  • 南京网站建设公司点击器原理
  • 网站怎么编辑搜狗网站提交入口
  • 自建网站做外贸的流程广告推广方式
  • 警告欺骗网站模板免费注册
  • 获取网站访客信息seo分析师招聘
  • 制作网页的网站有哪些网站建设
  • 日本真人做爰无遮挡视频免费网站嘉兴关键词优化报价
  • 忻州市中小企业局网站贵州整站优化seo平台
  • 网页怎么制作超链接seo兼职接单平台
  • 网站建设中应注意哪些问题重庆整站seo