View Full Version : setchecked( true) of the radio button doesn't work
richardander
27th January 2009, 18:57
Hello,
In the UI of my application, there are several radio buttons. They work well when I click them.
However, when I use raidoButton_1->setChecked(true) to set them, UI doesn't change. Shall I call some other functions to update them?
Thank you!
jpn
27th January 2009, 21:08
However, when I use raidoButton_1->setChecked(true) to set them, UI doesn't change. Shall I call some other functions to update them?
No, there is no need. It should work unless you block the event loop somehow. Do you have a busy loop in your application?
richardander
28th January 2009, 00:42
There is no busy loop in the application.
Could you let me know what are the commands/functions in QT that can block the event loop?
Thank you!
jpn
28th January 2009, 06:54
Does this work for you?
// main.cpp
#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QRadioButton button1("checked");
QRadioButton button2("unchecked");
button1.setChecked(true);
button2.setChecked(false);
button1.show();
button2.show();
return app.exec();
}
Are you sure that the code where you change the check state gets actually executed?
richardander
28th January 2009, 07:59
Hello,
The code works. In fact, I did another test: put three radio buttons in one qgroupbox. Using setChecked( true) to select one radio button. But, if I want to clear the selection, the setChecked( false ) seems not working. In following code, first radio button is selected. But it can not be unselected.
// main.cpp
#include <QtGui>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QGroupBox *groupBox = new QGroupBox("Exclusive Radio Buttons");
QRadioButton *radio1 = new QRadioButton("&Radio button 1");
QRadioButton *radio2 = new QRadioButton("R&adio button 2");
QRadioButton *radio3 = new QRadioButton("Ra&dio button 3");
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addWidget(radio3);
vbox->addStretch(1);
groupBox->setLayout(vbox);
groupBox->show();
radio1->setChecked(true);
radio1->setChecked(false);
return app.exec();
}
Is there any way to make no radiobutton selected?
Thank you!
spirit
28th January 2009, 08:01
put needed radiobutons in QGroupBox.
richardander
28th January 2009, 08:17
As shown in the above sample code, the three radio buttons have been put in the QGroupBox so that only one radiobutton can be selected.
however, if I want to clear the current selection and make all radiobutton unselected, what shall I do?
thank you!
spirit
28th January 2009, 08:21
try to add the following code to yours
...
radio1->setCheckable(false);
radio2->setCheckable(false);
radio3->setCheckable(false);
...
but in this case you can't be able to check buttons at all.
jpn
28th January 2009, 08:59
Radio buttons are "auto-exclusive" by default. If auto-exclusive is enabled, radio buttons that belong to the same parent widget behave as if they were part of the same exclusive button group.
richardander
28th January 2009, 17:54
Thank you!
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.