user225312
user225312

Reputation: 131577

QRadioButton: Setting all radio buttons in a group to unchecked state

I have three radio buttons, let's call them R1, R2 and R3. (R1 is in the checked set)

My problem is that I have a method called check() that gets the current radio button using:

def check(self):
    if R1.isChecked():
      # 
    if R2.isChecked():
      # 
    if R3.isChecked():
      #

Based on which radio button is active, the appropriate method is further called.

However the problem with this approach is that when the form loads up, R1 is checked. Then when I call the check(), since R1 is already checked when the form loads, it just returns R1 always.

What would be the way to fix this? I want that depending on the choice of the user, the appropriate method be called.

So I was wondering whether it is possible to have no radio button checked when the form loads up?

Upvotes: 1

Views: 27589

Answers (1)

Naruto
Naruto

Reputation: 9634

hope this could help you, initially all the radio buttons will be in unchecked state.

     QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));
     QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
     QObject::connect(radio1,SIGNAL(clicked(bool)),this,SLOT(clickkedstate(bool)));
     radio1->setAutoExclusive(false);
     QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
     QObject::connect(radio2,SIGNAL(clicked(bool)),this,SLOT(clickkedstate(bool)));
     radio2->setAutoExclusive(false);
     QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
     radio3->setAutoExclusive(false);
     QObject::connect(radio3,SIGNAL(clicked(bool)),this,SLOT(clickkedstate(bool)));

     radio1->setChecked(false);
     radio2->setChecked(false);
     radio3->setChecked(false);


     QVBoxLayout *vbox = new QVBoxLayout;
     vbox->addWidget(radio1);
     vbox->addWidget(radio2);
     vbox->addWidget(radio3);
     vbox->addStretch(1);
     groupBox->setLayout(vbox);
     setLayout(vbox);

Upvotes: 4

Related Questions