QComboBox实现复选功能(三种方法:嵌套QListWidget, 设置QStandardItemModel, 设置Delegate)
今天介绍一下一个小东西 — 如何让QComboBox实现复选功能?
- 下拉列表有复选功能
- 不可编辑
- 显示所有选中项
- QStandardItemModel + QStandardItem
- QListWidget + QListWidgetItem
- Model/View + QItemDelegate
pListWidget = new QListWidget(this);
pLineEdit = new QLineEdit(this);
for (int i = 0; i < 5; ++i)
{
QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
pListWidget->addItem(pItem);
pItem->setData(Qt::UserRole, i);
QCheckBox *pCheckBox = new QCheckBox(this);
pCheckBox->setText(QStringLiteral("Qter%1").arg(i));
pListWidget->addItem(pItem);
pListWidget->setItemWidget(pItem, pCheckBox);
connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
}
ui.comboBox->setModel(pListWidget->model());
ui.comboBox->setView(pListWidget);
ui.comboBox->setLineEdit(pLineEdit);
pLineEdit->setReadOnly(true); //ui.comboBox->setEditable(true);
connect(pLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
void MultiComboBox::stateChanged(int state)
{
bSelected = true;
QString strSelectedData("");
strSelectedText.clear();
QObject *object = QObject::sender();
QCheckBox *pSenderCheckBox = static_cast(object);
int nCount = pListWidget->count();
for (int i = 0; i < nCount; ++i)
{
QListWidgetItem *pItem = pListWidget->item(i);
QWidget *pWidget = pListWidget->itemWidget(pItem);
QCheckBox *pCheckBox = (QCheckBox *)pWidget;
if (pCheckBox->isChecked())
{
QString strText = pCheckBox->text();
strSelectedData.append(strText).append(";");
}
//所点击的复选框
if (pSenderCheckBox == pCheckBox)
{
int nData = pItem->data(Qt::UserRole).toInt();
qDebug() << QString("I am sender...id : %1").arg(nData);
}
}
if (strSelectedData.endsWith(";"))
strSelectedData.remove(strSelectedData.count() - 1, 1);
if (!strSelectedData.isEmpty())
{
//ui.comboBox->setEditText(strSelectedData);
strSelectedText = strSelectedData;
pLineEdit->setText(strSelectedData);
pLineEdit->setToolTip(strSelectedData);
}
else
{
pLineEdit->clear();
//ui.comboBox->setEditText("");
}
bSelected = false;
}
void MultiComboBox::textChanged(const QString &text)
{
if (!bSelected)
pLineEdit->setText(strSelectedText);
}
QComboBox实现复选功能(三种方法:嵌套QListWidget, 设置QStandardItemModel, 设置Delegate)的更多相关文章
- QComboBox实现复选功能
需求: 下拉列表有复选功能 不可编辑 显示所有选中项 关于QComboBox的复选功能有几种方案: QStandardItemModel + QStandardItem QListWidget + ...
- QTreeView/QTableView中利用QStandardItem实现复选框三种形态变化
https://www.techieliang.com/2017/12/729/ 原文地址 using_checkbox_item.h /** * @file using_checkbox_item. ...
- Qt 设置背景图片3种方法(三种方法:QPalette调色板,paintEvent,QSS)
方法1. setStylSheet{"QDialog{background-image:url()"}} //使用styleSheet 这种方法的好处是继承它的dialog都会自 ...
- Java实现ping功能的三种方法及Linux的区分
前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要 ...
- QT 创建一个具有复选功能的下拉列表控件
最近研究了好多东西,前两天突然想做一个具有复选功能的下拉列表框.然后在网上"学习"了很久之后,终于发现了一个可以用的,特地发出来记录一下. 一.第一步肯定是先创建一个PROJECT ...
- mac学习Python第一天:安装、软件说明、运行python的三种方法
一.Python安装 从Python官网下载Python 3.x的安装程序,下载后双击运行并安装即可: Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的. MAC 系统一般 ...
- 痞子衡嵌入式:大话双核i.MXRT1170之在线联合调试双核工程的三种方法(IAR篇)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是i.MXRT1170下在线联合调试双核工程的方法(基于IAR). 前段时间痞子衡写过一篇<双核i.MXRT1170之单独在线调试从 ...
- AJAX实现跨域的三种方法
由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问. 比如说你的网站域名是aaa.com,想要通过AJAX请 ...
- YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法
上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...
随机推荐
- mysql常见故障诊断
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010230971/article/details/80335578 作为故障预警,应该尽量把问题扼 ...
- 设置cell背景色和选中色
// 设置cell的背景色 UIView *bg = [[[UIView alloc] init] autorelease]; bg.backgroundColor = [UIColor colorW ...
- 手动脱KBys Packer(0.28)壳实战
作者:Fly2015 吾爱破解培训第一课选修作业第5个练习程序.在公司的时候用郁金香OD调试该加壳程序的时候出了点问题,可是回家用吾爱破解版的OD一调试,浑身精神爽,啥问题也没有. 首先使用查壳工具对 ...
- 洛谷 P3871 中位数
->题目链接 题解: 暴力 经鉴定,此题数据水到没朋友. #include<algorithm> #include<iostream> #include<cstdi ...
- [Angular2 Animation] Use Keyframes for Fine-Tuned Angular 2 Animations
When easing isn’t enough to get the exact animation you want, you can leverage keyframes to define a ...
- Gora快速入门 分类: C_OHTERS 2015-01-30 09:55 465人阅读 评论(0) 收藏
概述 Gora是apache的一个开源项目. The Apache Gora open source framework provides an in-memory data model and pe ...
- Delphi的指针(有图,很清楚)
Pointers are like jumps, leading wildly from one part of the data structure to another. Their introd ...
- LOG4J中日志级别的使用
<logger name="demo-log" additivity="false"> <level value="${log.le ...
- [Elm] Installing and setting up Elm
Before writing any Elm we need to first install the runtime locally. In this lesson we install the E ...
- C#趣味程序---个位数为6,且能被3整出的五位数
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int ...