当前位置:网站首页>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
边栏推荐
- 线程安全的单例模式
- Machine learning process and method
- 微信小程序開發工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理問題
- [Yu Yue education] reference materials of chemical experiment safety knowledge of University of science and technology of China
- awk从入门到入土(3)awk内置函数printf和print实现格式化打印
- Button button adaptive size of wechat applet
- File class (add / delete)
- Leetcode (540) -- a single element in an ordered array
- Awk from entry to burial (1) awk first meeting
- Wechat applet development tool post net:: err_ PROXY_ CONNECTION_ Failed agent problem
猜你喜欢
Processing of tree structure data
Detailed introduction to the deployment and usage of the Nacos registry
How can retail enterprises open the second growth curve under the full link digital transformation
What are the key points often asked in the redis interview
通达OA v12流程中心
awk从入门到入土(0)awk概述
Thread safe singleton mode
Depth (penetration) selector:: v-deep/deep/ and > > >
y54.第三章 Kubernetes从入门到精通 -- ingress(二七)
[shutter] bottom navigation bar implementation (bottomnavigationbar bottom navigation bar | bottomnavigationbaritem navigation bar entry | pageview)
随机推荐
各国Web3现状与未来
Socket programming
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
力扣(LeetCode)183. 从不订购的客户(2022.07.02)
String replace space
Kotlin middle process understanding and Practice (II)
How to deal with cache hot key in redis
浏览器是如何对页面进行渲染的呢?
Servlet中数据传到JSP页面使用el表达式${}无法显示问题
udp接收队列以及多次初始化的测试
[shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
[fluent] fluent debugging (debug debugging window | viewing mobile phone log information | setting normal breakpoints | setting expression breakpoints)
Memory pool (understand the process of new developing space from the perspective of kernel)
Trial setup and use of idea GoLand development tool
Leetcode(540)——有序数组中的单一元素
机器学习流程与方法
Awk from getting started to getting into the ground (3) the built-in functions printf and print of awk realize formatted printing
In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?
Button button adaptive size of wechat applet
[shutter] shutter debugging (debugging fallback function | debug method of viewing variables in debugging | console information)