当前位置:网站首页>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^/)
注:文章为作者编程过程中所遇到的问题和总结,内容仅供参考,若有错误欢迎指出。
注:如有侵权,请联系作者删除
边栏推荐
- Answers to ten questions about automated testing software testers must see
- leetcode961. Find the elements repeated N times in the array with length 2n
- Kotlin middle process understanding and Practice (II)
- 2022 financial product revenue ranking
- 浏览器是如何对页面进行渲染的呢?
- Prohibited package name
- Basic operation of view
- stm32F407-------DMA
- Startup mode and scope builder of collaboration in kotlin
- Network security - the simplest virus
猜你喜欢
MySQL learning 03
Redis: simple use of redis
使用Go语言实现try{}catch{}finally
Everything file search tool
Recommendation letter of "listing situation" -- courage is the most valuable
8 free, HD, copyright free video material download websites are recommended
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
[shutter] shutter debugging (debugging control related functions | breakpoint management | code operation control)
随机推荐
How to deal with cache hot key in redis
502 (bad gateway) causes and Solutions
ByteDance data Lake integration practice based on Hudi
File class (check)
iptables 4层转发
树形结构数据的处理
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
Storage basic operation
[shutter] pull the navigation bar sideways (drawer component | pageview component)
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
[leetcode] 797 and 1189 (basis of graph theory)
8 free, HD, copyright free video material download websites are recommended
[shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)
How do it students find short-term internships? Which is better, short-term internship or long-term internship?
Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
机器学习笔记(持续更新中。。。)
Y54. Chapter III kubernetes from introduction to mastery -- ingress (27)
Deep learning notes (constantly updating...)
CFdiv2-Fixed Point Guessing-(区间答案二分)
Network security - vulnerabilities and Trojans