当前位置:网站首页>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
边栏推荐
- Servlet中数据传到JSP页面使用el表达式${}无法显示问题
- Leetcode 183 Customers who never order (2022.07.02)
- CFdiv2-Fixed Point Guessing-(区间答案二分)
- Recommendation letter of "listing situation" -- courage is the most valuable
- How can retail enterprises open the second growth curve under the full link digital transformation
- 苏世民:25条工作和生活原则
- [Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
- Kotlin middle process understanding and Practice (II)
- 缺少库while loading shared libraries: libisl.so.15: cannot open shared object file: No such file
- Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
猜你喜欢

深度(穿透)选择器 ::v-deep/deep/及 > > >
![[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数](/img/06/ab333a4752de27eae2dd937cf579e2.png)
[Flutter] dart: class;abstract class;factory;类、抽象类、工厂构造函数

Ni visa fails after LabVIEW installs the third-party visa software

stm32F407-------ADC

Comment communiquer avec Huawei Cloud IOT via le Protocole mqtt

微服务组件Sentinel (Hystrix)详细分析

MySQL learning 03

微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題

SPI机制
![[camera topic] turn a drive to light up the camera](/img/d3/7aabaa5c75813abc4a43820b4c3706.png)
[camera topic] turn a drive to light up the camera
随机推荐
Stm32f407 ------- IIC communication protocol
Anna: Beibei, can you draw?
Leetcode (540) -- a single element in an ordered array
Awk from getting started to getting into the ground (3) the built-in functions printf and print of awk realize formatted printing
The Sandbox阐释对元宇宙平台的愿景
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
Prohibited package name
Introduce in detail how to communicate with Huawei cloud IOT through mqtt protocol
Pytorch convolution network regularization dropblock
Depth (penetration) selector:: v-deep/deep/ and > > >
What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
leetcode961. Find the elements repeated N times in the array with length 2n
Socket编程
詳細些介紹如何通過MQTT協議和華為雲物聯網進行通信
4. 类和对象
Missing library while loading shared libraries: libisl so. 15: cannot open shared object file: No such file
Summary of ES6 filter() array filtering methods
【CodeForces】CF1338A - Powered Addition【二进制】
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)
Ni visa fails after LabVIEW installs the third-party visa software