Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGroupBox, QCheckBox, select several options.
Forum Update on Monday, May 27th 2025

QGroupBox, QCheckBox, select several options.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 343 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.
  • lincolnL Offline
    lincolnL Offline
    lincoln
    wrote on last edited by lincoln
    #1

    Hi guys, I have this code, in my form I have two QGroupBox with different alternatives, as you can see in the code what I try to show in a message the options that the user chose, but when I click on my button the program simply closes .

    Here my code.

    #ifndef GROUPBOXDIALOG_H
    #define GROUPBOXDIALOG_H
    
    #include <QDialog>
    #include <QMessageBox>
    #include <QGroupBox>
    
    namespace Ui {
      class GroupBoxDialog;
    }
    
    class GroupBoxDialog : public QDialog
    {
      Q_OBJECT
    
    public:
      explicit GroupBoxDialog(QWidget *parent = nullptr);
      ~GroupBoxDialog();
    
    private slots:
      void on_buttonBox_accepted();
    
      void on_buttonBox_rejected();
    
    
    
    private:
      Ui::GroupBoxDialog *ui;
      QString getOptions(QGroupBox *group);
    };
    
    #endif // GROUPBOXDIALOG_H
    
    
    #include "groupboxdialog.h"
    #include "ui_groupboxdialog.h"
    
    GroupBoxDialog::GroupBoxDialog(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::GroupBoxDialog)
    {
      ui->setupUi(this);
    }
    
    GroupBoxDialog::~GroupBoxDialog()
    {
      delete ui;
    }
    
    void GroupBoxDialog::on_buttonBox_accepted()
    {
      QString message;
    
      QString food=getOptions(ui->groupBox_3);
      message.append(food);
    
      QString activities=getOptions(ui->groupBox_4);
      message.append(activities);
    
      QMessageBox::information(this,"Details",message);
      accept();
    }
    
    void GroupBoxDialog::on_buttonBox_rejected()
    {
      reject();
    }
    
    QString GroupBoxDialog::getOptions(QGroupBox *group)
    {
      QString value;
      foreach(QObject *object, group->children())
        {
          QCheckBox *chk=qobject_cast<QCheckBox*>(object);
          if(chk) continue;
          if(chk->isChecked()) value.append(chk->text()+"\r\n");
        }
      return value;
    }
    
    
    
    

    When the application closes, it shows me this message.

    b9d580da-14ec-41b3-bc81-9763b838a299-image.png

    my form:

    134bd75a-5a3c-46c0-a378-3f8cdaf301df-image.png

    Solitary wolf

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @lincoln said in QGroupBox, QCheckBox, select several options.:

      when I click on my button

      Which button?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • lincolnL lincoln

        Hi guys, I have this code, in my form I have two QGroupBox with different alternatives, as you can see in the code what I try to show in a message the options that the user chose, but when I click on my button the program simply closes .

        Here my code.

        #ifndef GROUPBOXDIALOG_H
        #define GROUPBOXDIALOG_H
        
        #include <QDialog>
        #include <QMessageBox>
        #include <QGroupBox>
        
        namespace Ui {
          class GroupBoxDialog;
        }
        
        class GroupBoxDialog : public QDialog
        {
          Q_OBJECT
        
        public:
          explicit GroupBoxDialog(QWidget *parent = nullptr);
          ~GroupBoxDialog();
        
        private slots:
          void on_buttonBox_accepted();
        
          void on_buttonBox_rejected();
        
        
        
        private:
          Ui::GroupBoxDialog *ui;
          QString getOptions(QGroupBox *group);
        };
        
        #endif // GROUPBOXDIALOG_H
        
        
        #include "groupboxdialog.h"
        #include "ui_groupboxdialog.h"
        
        GroupBoxDialog::GroupBoxDialog(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::GroupBoxDialog)
        {
          ui->setupUi(this);
        }
        
        GroupBoxDialog::~GroupBoxDialog()
        {
          delete ui;
        }
        
        void GroupBoxDialog::on_buttonBox_accepted()
        {
          QString message;
        
          QString food=getOptions(ui->groupBox_3);
          message.append(food);
        
          QString activities=getOptions(ui->groupBox_4);
          message.append(activities);
        
          QMessageBox::information(this,"Details",message);
          accept();
        }
        
        void GroupBoxDialog::on_buttonBox_rejected()
        {
          reject();
        }
        
        QString GroupBoxDialog::getOptions(QGroupBox *group)
        {
          QString value;
          foreach(QObject *object, group->children())
            {
              QCheckBox *chk=qobject_cast<QCheckBox*>(object);
              if(chk) continue;
              if(chk->isChecked()) value.append(chk->text()+"\r\n");
            }
          return value;
        }
        
        
        
        

        When the application closes, it shows me this message.

        b9d580da-14ec-41b3-bc81-9763b838a299-image.png

        my form:

        134bd75a-5a3c-46c0-a378-3f8cdaf301df-image.png

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @lincoln
        Apart from what @Christian-Ehrlicher has asked you....

        Have you attached the two slots in your ui->setupUi(this);?

        I assume so. Then you are crashing on if(chk->isChecked()) because of this line:

        if(chk) continue;

        You mean if(!chk) continue;!

        1 Reply Last reply
        3
        • lincolnL Offline
          lincolnL Offline
          lincoln
          wrote on last edited by
          #4

          thanks, I already solved the problem, it was just changing the condition in the if as jonB said.

          Solitary wolf

          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