Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. how to check the status of checkboxes in a groupbox
Forum Updated to NodeBB v4.3 + New Features

how to check the status of checkboxes in a groupbox

Scheduled Pinned Locked Moved Solved The Lounge
7 Posts 2 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fatemehkarimi
    wrote on last edited by
    #1

    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???

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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();
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      F 2 Replies Last reply
      2
      • F Offline
        F Offline
        fatemehkarimi
        wrote on last edited by fatemehkarimi
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • VRoninV VRonin

          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();
          
          F Offline
          F Offline
          fatemehkarimi
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • VRoninV VRonin

            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();
            
            F Offline
            F Offline
            fatemehkarimi
            wrote on last edited by
            #5

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

            VRoninV 1 Reply Last reply
            0
            • F fatemehkarimi

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

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              F 1 Reply Last reply
              3
              • VRoninV VRonin

                @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

                F Offline
                F Offline
                fatemehkarimi
                wrote on last edited by
                #7

                @VRonin
                thank you extremely much.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved