how to check the status of checkboxes in a groupbox
Solved
The Lounge
-
I have a number of checkboxes inside a QGroupBox.
It looks something like this--GroupBox----------------
| 0 - checkbox1 |
| 0 - checkbox2 |
| 0 - checkbox3 |
| 0 - checkbox4 |
| 0 - checkbox5 | | ok |check boxes are added dynamically to group box, so the number of them is unknown at compile time.
each checkbox presents a student name and is checked if that student is present in the class.when ok is pressed, I must check which checkboxes are checked. how can I do this???
-
see also https://forum.qt.io/topic/79617/accessing-members-in-groupbox
- you are doing it wrong. You should use a checkable QListWidget (or, even better, QListView+ QStandardItemModel)
- you can use findChildren to do it in your current state:
const auto checkBoxList = groupBox->findChildren<QCheckBox*>(); for(auto&& singleBox : checkBoxList) qDebug() << singleBox->text() << singleBox->isChecked();
-
This post is deleted!
-
@fatemehkarimi said in how to check the status of checkboxes in a groupbox:
invalid use of incomplete type 'class QDebug'
that means you forgot
#include <QDebug>
what do you mean by checkable QListWidget
http://doc.qt.io/qt-5/qlistwidget.html
and instead of:
QGroupBox *students = new QGroupBox; students->setTitle("List Of Students"); QVBoxLayout * vlayout = new QVBoxLayout;
for (int i = 0; i < 25; ++i) { QCheckBox * newStudent = new QCheckBox; newStudent->setText("fatemeh karimi"); vlayout->addWidget(newStudent); }
use
QListWidget *students = new QListWidget;
for (int i = 0; i < 25; ++i) { QListWidgetItem* newStudent = new QListWidgetItem("fatemeh karimi",students); newStudent ->setFlags(Qt::ItemIsUserCheckable | newStudent->flags()); }
and then you can easily iterate through the items of the QListWidget