[Qt-interest] check box problem
K. Frank
kfrank29.c at gmail.com
Wed May 12 16:30:09 CEST 2010
Hello Ramesh (and Andre) -
On Wed, May 12, 2010 at 7:48 AM, Andre Somers <andre at familiesomers.nl> wrote:
> On 12-5-2010 13:35, Ramesh wrote:
>
> Hi, all
>
> What is the mistake in this code,
>
> Initially, all the check boxes will be selected, later if I click first
> checkbox, it will get untick, later if I click on the first box again, it
> will get selected but the second one will be unticked.. similarly if I
> reaped the process, the third box also get unticked.. my I know where I am
> wrong…
> ...
>
> That is the expected behaviour from radio buttons. You did not specify what
> kind of behaviour you are looking for, so I have no idea what you want to
> achieve. Perhaps you should use QCheckBox instead? QRadioButtons are
> supposed to be mutually exclusive.
>
> André
I agree with Andre: It's not clear what behavior you are expecting, so I'm not
sure what you're asking.
As Andre says, if you don't want your buttons to be mutually exclusive, use
something like QCheckBox.
However, I do see in your sample code a reference to "exclusive"...
> Here is the code L
> ...
>
> RadioButtonwidget::RadioButtonwidget(QWidget *parent)
> : QWidget(parent)
> {
>
> QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));
> QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
> ...
Because you are labelling your QGroupBox "Exclusive Radio Buttons", I will
assume that you do want radio buttons with their exclusive behavior. (I don't,
however, think your QGroupBox actually gets used in your sample code.)
> ...
> radio1->setChecked(true);
> radio2->setChecked(true);
> radio3->setChecked(true);
This, I think, is your problem.
You set all of your radio buttons to their checked state, violating
the exclusive
"invariant". (It's not fully invariant, because it's not fully
enforced by QRadioButton,
as your example shows.)
So your RadioButtonwidget displays initially with all of the buttons
checked, so they
don't act quite right.
If you want your radio buttons to act normally, you should start out
with exactly on
checked, e.g.:
radio1->setChecked(true);
radio2->setChecked(false);
radio3->setChecked(false);
Normally you can't uncheck a radio button (that is in a group of more
than one) by
clicking on it, but when you start out with more than one checked, you can.
That's the first minor oddity.
Then when re-checking the button you unchecked, rather than clearing all of
the rest of the radio buttons, it appears that Qt (I would guess for the sake of
a minor efficiency gain) unchecks only the the first checked button it finds in
the group of buttons.
So -- as you described -- click the first button twice, and the second button
is unchecked, click the first button twice again, and now the third button is
also unchecked.
Once you get all but one of the radio buttons checked, they should behave
as expected.
Good luck.
K. Frank
> ...
More information about the Qt-interest-old
mailing list