Solved QGroupBox, QCheckBox, select several options.
-
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.
my form:
-
@lincoln said in QGroupBox, QCheckBox, select several options.:
when I click on my button
Which button?
-
@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;
! -
thanks, I already solved the problem, it was just changing the condition in the if as jonB said.