Multiple QRadioButton::setChecked() in QDialog
-
I have created a QDialog subclass that includes to sets of QRadioButtons. Each of these sets is defined by using QGroupBox and for each QGroupBox, I have set one of the QRadioButtons to be checked (by using QRadioButton::setChecked(true). However, when executing the code, only the QRadioButton for which the setChecked function is called last is actually visibly checked while for the other, its status is not visualized. See picture.
The code corresponding to the above QDialog looks as follows:// Create Buttons Minimum = new QRadioButton(tr("Minimum"), this); Average = new QRadioButton(tr("Average"), this); Maximum = new QRadioButton(tr("Maximum"), this); Maximum->setChecked(true); QRadioButton *Instantiated = new QRadioButton(tr("Instantiated Process Classes Only"), this); Instantiated->setChecked(true); All = new QRadioButton(tr("All Process Classes"), this); QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(ButtonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(ButtonBox, SIGNAL(rejected()), this, SLOT(reject())); // Layout QGroupBox *Fix = new QGroupBox(tr("Replace Execution Time Distributions by"), this); QVBoxLayout *FixLayout = new QVBoxLayout; FixLayout->addWidget(Minimum); FixLayout->addWidget(Average); FixLayout->addWidget(Maximum); Fix->setLayout(FixLayout); QGroupBox *Classes = new QGroupBox(tr("For behavior of"), this); QVBoxLayout *ClassLayout = new QVBoxLayout; ClassLayout->addWidget(Instantiated); ClassLayout->addWidget(All); Classes->setLayout(ClassLayout); QVBoxLayout *Layout = new QVBoxLayout; Layout->addWidget(Fix); Layout->addWidget(Classes); Layout->addWidget(ButtonBox); setLayout(Layout); setWindowTitle(tr("Fix Execution Times"));
Any ideas on how to make sure that both programmatically selected QRadioButtons are visibly checked?
-
Hi, I think because all 5 of the QRadioButtons share the parent widget only 1 can be active. Try reshuffling your code so that the first 3 buttons change owner to the Fix groupbox, then after that create the 2 button after that, like this:
... // Create Buttons Minimum = new QRadioButton(tr("Minimum"), this); Average = new QRadioButton(tr("Average"), this); Maximum = new QRadioButton(tr("Maximum"), this); Maximum->setChecked(true); QGroupBox *Fix = new QGroupBox(tr("Replace Execution Time Distributions by"), this); QVBoxLayout *FixLayout = new QVBoxLayout; FixLayout->addWidget(Minimum); FixLayout->addWidget(Average); FixLayout->addWidget(Maximum); Fix->setLayout(FixLayout); QRadioButton *Instantiated = new QRadioButton(tr("Instantiated Process Classes Only"), this); Instantiated->setChecked(true); All = new QRadioButton(tr("All Process Classes"), this); QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(ButtonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(ButtonBox, SIGNAL(rejected()), this, SLOT(reject())); QGroupBox *Classes = new QGroupBox(tr("For behavior of"), this); QVBoxLayout *ClassLayout = new QVBoxLayout; ClassLayout->addWidget(Instantiated); ClassLayout->addWidget(All); Classes->setLayout(ClassLayout); ...
Edit: instead of reshuffling, another solution is to create 2 QButtonGroup instances, insert the first 3 radiobuttons in one of them and 2 in the other (i.e. another way to split the owner of the buttons).
-
@hskoglund Thanks for the hint to change the ownership. That solved the problem. I have now the following code:
// Create Buttons QGroupBox *Fix = new QGroupBox(tr("Replace Execution Time Distributions by"), this); Minimum = new QRadioButton(tr("Minimum"), Fix); Average = new QRadioButton(tr("Average"), Fix); Maximum = new QRadioButton(tr("Maximum"), Fix); Maximum->setChecked(true); QGroupBox *Classes = new QGroupBox(tr("For behavior of"), this); All = new QRadioButton(tr("All Process Classes"), Classes); QRadioButton *Instantiated = new QRadioButton(tr("Instantiated Process Classes Only"), Classes); Instantiated->setChecked(true); QDialogButtonBox *ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(ButtonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(ButtonBox, SIGNAL(rejected()), this, SLOT(reject())); // Layout QVBoxLayout *FixLayout = new QVBoxLayout; FixLayout->addWidget(Minimum); FixLayout->addWidget(Average); FixLayout->addWidget(Maximum); Fix->setLayout(FixLayout); QVBoxLayout *ClassLayout = new QVBoxLayout; ClassLayout->addWidget(Instantiated); ClassLayout->addWidget(All); Classes->setLayout(ClassLayout); QVBoxLayout *Layout = new QVBoxLayout; Layout->addWidget(Fix); Layout->addWidget(Classes); Layout->addWidget(ButtonBox); setLayout(Layout); setWindowTitle(tr("Fix Execution Times"));