QRadioButton check/uncheck issue in Qt
Asked Answered
qt
M

2

10

I am finding issues related to check/uncheck of QRadioButton. The images I have used for checking(a white dot) and unchecking(without a white dot) is not updated. My issue is like: I have implemented few QRadioButton(s). For the first time all the QRadioButtons checked false. So the images for this case is without a white dot. When user selects any QRadioButton then it's image changes to another i.e. image with a white dot. On a button click I am resetting the state of the radio buttons from checked to uncheck state . However the images state are not changing. They remain in checked state. The code snippet is as follows:

Code:

if(ui->radioButtonReadOnlineData->isChecked())
    ui->radioButtonReadOnlineData->setChecked(false);
if(ui->radioButtonSavetoDBReadOfflineData->isChecked())
    ui->radioButtonSavetoDBReadOfflineData->setChecked(false);
if(ui->radioButtonViewLocalData->isChecked())
    ui->radioButtonViewLocalData->setChecked(false);
if(ui->radioButtonDateRange->isChecked())
    ui->radioButtonDateRange->setChecked(false);
if(ui->radioButtonAll->isChecked())
    ui->radioButtonAll->setChecked(false);

The images for each of the QRadioButtons is set as like:

Code:

ui->radioButtonAll->setStyleSheet(
            "QRadioButton::indicator::checked { image: url(:/Resources/radio-btn-selected.png);}"
            "QRadioButton::indicator::unchecked {image: url(:/Resources/radio-btn-unselected.png);}"
            );

Any clues why the QRradioButton images are not updated. Thanks.

Mills answered 21/2, 2012 at 6:8 Comment(0)
P
19

Your problem is most probably related to

setAutoExclusive(bool)

By default all buttons belonging to the same parent behave as if they were part of the same exclusive button group. After having selected one you are not able to return to having all buttons unchecked.

A work around is to find out what button is checked, and for that button do the following

theSelectedButton->setAutoExclusive(false);
thsSelectedButton->setChecked(false);
theSelectedButton->setAutoExclusive(true);

Take a look at these links for more information:

http://developer.qt.nokia.com/forums/viewthread/5482

http://www.qtforum.org/article/19619/qradiobutton-setchecked-bug.html

Pablopabon answered 21/2, 2012 at 9:59 Comment(3)
the following code works with one issue: theSelectedButton->setCheckable(false); thsSelectedButton->setChecked(false); theSelectedButton->setCheckable(true); issue is that previously selected radio button appears when you select a new radio button. How would I prevent this? please guide me.Mills
The updated code is as follows: if(ui->radioButtonViewLocalData->isChecked()) { //ui->radioButtonViewLocalData->setAutoExclusive(false); ui->radioButtonViewLocalData->setChecked(false); //ui->radioButtonViewLocalData->setAutoExclusive(true); ui->radioButtonViewLocalData->setCheckable(false); ui->radioButtonViewLocalData->update(); ui->radioButtonViewLocalData->setCheckable(true); } Mills
In addition to this answer, if your buttons are part of a QButtonGroup, you will need to use the button group "setExclusive" function instead: theSelectedButtonGroup->setExclusive(false);Throstle
A
0

Make sure your resource file look like :

<qresource>
   <file>Resources/radio-btn-selected.png</file>
   <file>Resources/radio-btn-unselected.png</file>
</qresource>

And that it is included correctly in your application.

  • Either include the .qrc in your .pro file with
 RESOURCES = myresource.qrc
  • either create an external binary resource file, then register it at runtime with
QResource::registerResource("/path/to/myresource.rcc");
  • Or if you are using the designer, you can do like this.
Asmodeus answered 21/2, 2012 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.