10
votes

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.

2

2 Answers

18
votes

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

0
votes

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.