当前位置:网站首页>Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
2022-07-03 02:02:00 【lw只吃亿点.】
一、下拉列表框插入复选框示例图
下图为带复选框的下拉列表框示例图,其中包含添加项,选中项,勾选复选框等操作。
提示:不会使用Qt设计师设计界面的小伙伴点击这里
二、源码
ComboBoxTest.h
#ifndef COMBOBOXTEST_H
#define COMBOBOXTEST_H
#include <QWidget>
#include <QListWidget>
namespace Ui {
class ComboBoxTest;
}
class ComboBoxTest : public QWidget
{
Q_OBJECT
public:
explicit ComboBoxTest(QWidget *parent = nullptr);
~ComboBoxTest();
private slots:
/** * @brief on_addBtn_clicked 添加item按钮点击槽函数 */
void on_addBtn_clicked();
/** * @brief on_comboBox_currentIndexChanged 下拉列表框切换选项槽函数 * @param index 切换后的index索引位置 */
void on_comboBox_currentIndexChanged(int index);
private:
Ui::ComboBoxTest *ui;
QListWidget *m_listWidget; // 下拉列表框的
};
#endif // COMBOBOXTEST_H
ComboBoxTest.cpp
#include "ComboBoxTest.h"
#include "ui_ComboBoxTest.h"
#include <QCheckBox>
ComboBoxTest::ComboBoxTest(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ComboBoxTest)
{
ui->setupUi(this);
// 设置窗口标题
this->setWindowTitle("ComboBox带复选框");
// 创建QListWidget对象空间
m_listWidget = new QListWidget;
// 要先设置model,然后在设置view,我测试时反过来设置会崩掉哦
//! 将comboBox的model对象设置为QListWidget对象的model
//! 因为QListWidget继承QListView所以其存在model对象
ui->comboBox->setModel(m_listWidget->model());
//! 将comboBox的view对象设置为QListWidget对象
//! 因为QListWidget继承QListView所以相当于View
ui->comboBox->setView(m_listWidget);
//! 此处添加一个空item用于当默认值
// 创建QListWidgetItem对象并指定父对象
QListWidgetItem *item = new QListWidgetItem(m_listWidget);
// 将item添加到QListWidget对象中
m_listWidget->addItem(item);
}
ComboBoxTest::~ComboBoxTest()
{
//释放QListWidget、ui的存储空间
delete m_listWidget;
delete ui;
}
void ComboBoxTest::on_addBtn_clicked()
{
// 获取要添加item上显示的文本
QString text = ui->lineEdit->text();
// 当文本为空时直接返回,不添加
if(text.isEmpty())
{
return;
}
//! 创建复选框对象空间
//! 创建时指定复选框的父对象,其父对象在释放时会将其中的子对象指针先释放
QCheckBox *checkBox = new QCheckBox(m_listWidget);
// 创建QListWidgetItem对象并传入文本和指定父对象
QListWidgetItem *item = new QListWidgetItem(text, m_listWidget);
//设置item文本居中
item->setTextAlignment(Qt::AlignCenter);
//将item添加到QListWidget对象中
m_listWidget->addItem(item);
//将复选框设置到item中
m_listWidget->setItemWidget(item, checkBox);
}
void ComboBoxTest::on_comboBox_currentIndexChanged(int index)
{
//获取当前索引位置的item
QListWidgetItem *item = m_listWidget->item(index);
//当获取的item为空时返回
if(nullptr == item)
{
return;
}
//获取当前选项的文本
QString itemStr = item->text();
//获取当前选项所在复选框的widget对象
QWidget *widget = m_listWidget->itemWidget(item);
//将widget对象转换为checkBox对象
QCheckBox *checkBox = dynamic_cast<QCheckBox *>(widget);
//当获取的checkBox为空时返回
if(nullptr == checkBox)
{
ui->textBrowser->append("空");
return;
}
//根据checkBox的勾选状态获取文本
QString checkBoxStr = checkBox->isChecked()? "已勾选": "未勾选";
//组合text
QString text = QString("索引位置:%1 文本:%2 选中状态:%3")
.arg(index).arg(itemStr).arg(checkBoxStr);
//将文本追加到文本栏中
ui->textBrowser->append(text);
}
总结
在本文on_comboBox_currentIndexChanged获取到了复选框对象,可以在这里做出复选框应有的判断,或者在创建复选框时连接槽函数等操作;具体详解都在注释中,有意者细读哦。
友情提示——哪里看不懂可私哦,让我们一起互相进步吧
(创作不易,请留下一个免费的赞叭 谢谢 ^o^/)
注:文章为作者编程过程中所遇到的问题和总结,内容仅供参考,若有错误欢迎指出。
注:如有侵权,请联系作者删除
边栏推荐
- Asian Games countdown! AI target detection helps host the Asian Games!
- File class (add / delete)
- 小程序开发黑马购物商城中遇到的问题
- Leetcode (540) -- a single element in an ordered array
- Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
- Summary of ES6 filter() array filtering methods
- 浏览器是如何对页面进行渲染的呢?
- DDL basic operation
- 创建+注册 子应用_定义路由,全局路由与子路由
- 疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
猜你喜欢

8 free, HD, copyright free video material download websites are recommended

Depth (penetration) selector:: v-deep/deep/ and > > >

树形结构数据的处理

Use go language to realize try{}catch{}finally

MySQL learning 03

深度学习笔记(持续更新中。。。)

全链路数字化转型下,零售企业如何打开第二增长曲线
![[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)](/img/ac/bf83f319ea787c5abd7ac3fabc9ede.jpg)
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)

Asian Games countdown! AI target detection helps host the Asian Games!

Everything file search tool
随机推荐
去除网页滚动条方法以及内外边距
自定义组件、使用npm包、全局数据共享、分包
Network security - virus
8 free, HD, copyright free video material download websites are recommended
In the face of difficult SQL requirements, HQL is not afraid
Distributed transaction solution
In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
stm32F407-------DMA
Network security - Trojan horse
Coroutinecontext in kotlin
Detailed introduction to the deployment and usage of the Nacos registry
How to deal with cache hot key in redis
Explore the conversion between PX pixels and Pt pounds, mm and MM
[shutter] pull the navigation bar sideways (drawer component | pageview component)
A 30-year-old software tester, who has been unemployed for 4 months, is confused and doesn't know what to do?
Leetcode(540)——有序数组中的单一元素
Network security - password cracking
2022 financial product revenue ranking
Learn BeanShell before you dare to say you know JMeter
Exception handling in kotlin process