how to check the status of checkboxes in a groupbox



  • 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

    1. you are doing it wrong. You should use a checkable QListWidget (or, even better, QListView+ QStandardItemModel)
    2. 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!


  • @VRonin sorry but what do you mean by checkable QListWidget?? is it QList Widget in ui???



  • @VRonin

    the compiler says :

    E:\Fatemeh\Coding\Practice\test_widgetApp\mainwindow.cpp:48: error:  invalid use of incomplete type 'class QDebug'
         qDebug() << singleBox->text() << singleBox->isChecked();
                ^
    

    what should I do????



  • @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



  • @VRonin
    thank you extremely much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.