当前位置:网站首页>QT qcombobox add qccheckbox (drop-down list box insert check box, including source code + comments)
QT qcombobox add qccheckbox (drop-down list box insert check box, including source code + comments)
2022-07-03 02:16:00 【LW only eats 100 million points】
One 、 Drop down list box insert check box example figure
The following figure is an example of a drop-down list box with a check box , It contains added items , Checked items , Check the check box and other operations .
Tips : Can't use Qt Designer design interface partners click here
Two 、 Source code
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 add to item Click the slot function */
void on_addBtn_clicked();
/** * @brief on_comboBox_currentIndexChanged Drop down list box switch option slot function * @param index Switched index Index position */
void on_comboBox_currentIndexChanged(int index);
private:
Ui::ComboBoxTest *ui;
QListWidget *m_listWidget; // Drop down list box
};
#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);
// Set the window title
this->setWindowTitle("ComboBox With a check box ");
// establish QListWidget Object space
m_listWidget = new QListWidget;
// Set first model, And then in settings view, When I test, the reverse setting will collapse
//! take comboBox Of model Object is set to QListWidget Object's model
//! because QListWidget Inherit QListView So it exists model object
ui->comboBox->setModel(m_listWidget->model());
//! take comboBox Of view Object is set to QListWidget object
//! because QListWidget Inherit QListView So it's equivalent to View
ui->comboBox->setView(m_listWidget);
//! Add an empty... Here item Used when the default value
// establish QListWidgetItem Object and specify the parent object
QListWidgetItem *item = new QListWidgetItem(m_listWidget);
// take item Add to QListWidget In the object
m_listWidget->addItem(item);
}
ComboBoxTest::~ComboBoxTest()
{
// Release QListWidget、ui Storage space
delete m_listWidget;
delete ui;
}
void ComboBoxTest::on_addBtn_clicked()
{
// Get to add item The text displayed on the
QString text = ui->lineEdit->text();
// Return directly when the text is empty , Don't add
if(text.isEmpty())
{
return;
}
//! Create check box object space
//! Specify the parent object of the check box when creating , When its parent object is released, its child object pointer will be released first
QCheckBox *checkBox = new QCheckBox(m_listWidget);
// establish QListWidgetItem Object and pass in text and specify the parent object
QListWidgetItem *item = new QListWidgetItem(text, m_listWidget);
// Set up item Center text
item->setTextAlignment(Qt::AlignCenter);
// take item Add to QListWidget In the object
m_listWidget->addItem(item);
// Set the check box to item in
m_listWidget->setItemWidget(item, checkBox);
}
void ComboBoxTest::on_comboBox_currentIndexChanged(int index)
{
// Get the current index location item
QListWidgetItem *item = m_listWidget->item(index);
// When you get item Return for space time
if(nullptr == item)
{
return;
}
// Get the text of the current option
QString itemStr = item->text();
// Get the... Of the checkbox where the current option is located widget object
QWidget *widget = m_listWidget->itemWidget(item);
// take widget Object to checkBox object
QCheckBox *checkBox = dynamic_cast<QCheckBox *>(widget);
// When you get checkBox Return for space time
if(nullptr == checkBox)
{
ui->textBrowser->append(" empty ");
return;
}
// according to checkBox Get the text in the checked state of
QString checkBoxStr = checkBox->isChecked()? " Checked ": " Uncheck ";
// Combine text
QString text = QString(" Index position :%1 Text :%2 Selected state :%3")
.arg(index).arg(itemStr).arg(checkBoxStr);
// Append text to the text column
ui->textBrowser->append(text);
}
summary
In this article on_comboBox_currentIndexChanged Got the checkbox object , You can make the judgment that the checkbox should have here , Or connect the slot function when creating the check box ; The details are in the notes , Read carefully if you are interested .
Friendship tips —— Where can't you understand? It's private , Let's make progress together
( It's not easy to create , Please leave a free praise thank you ^o^/)
notes : This paper summarizes the problems encountered by the author in the process of programming , The content is for reference only , If there are any mistakes, please point out .
notes : If there is any infringement , Please contact the author for deletion
边栏推荐
- 疫情當頭,作為Leader如何進行團隊的管理?| 社區征文
- Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
- 微服务组件Sentinel (Hystrix)详细分析
- 【CodeForces】CF1338A - Powered Addition【二进制】
- Leetcode (540) -- a single element in an ordered array
- Redis:Redis的简单使用
- [shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)
- [Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数
- Analyzing several common string library functions in C language
- awk从入门到入土(2)认识awk内置变量和变量的使用
猜你喜欢
![[camera topic] how to save OTP data in user-defined nodes](/img/3e/b76c4d6ef9ab5f5b4326a3a8aa1c4f.png)
[camera topic] how to save OTP data in user-defined nodes
![[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)

Deep learning notes (constantly updating...)

Redis: simple use of redis

Recommendation letter of "listing situation" -- courage is the most valuable

Learn BeanShell before you dare to say you know JMeter

The technology boss is ready, and the topic of position C is up to you

easyExcel
![[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)](/img/e7/915404743d6639ac359bb4e7f7fbb7.jpg)
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
![[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数](/img/06/ab333a4752de27eae2dd937cf579e2.png)
[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数
随机推荐
Detailed analysis of micro service component sentinel (hystrix)
udp接收队列以及多次初始化的测试
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires
微服务组件Sentinel (Hystrix)详细分析
How to deal with cache hot key in redis
Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数
Flink CDC mongoDB 使用及Flink sql解析monggo中复杂嵌套JSON数据实现
PyTorch 卷积网络正则化 DropBlock
Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
easyExcel
awk从入门到入土(0)awk概述
Basic operation of view
File class (check)
人脸识别6- face_recognition_py-基于OpenCV使用Haar级联与dlib库进行人脸检测及实时跟踪
Codeforces Round #418 (Div. 2) D. An overnight dance in discotheque
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)
Unrecognized SSL message, plaintext connection?
Processing of tree structure data
UDP receive queue and multiple initialization test