PDA

View Full Version : QButtonGroup keyboard navigation



braindead
3rd September 2013, 10:18
Hello,

i stumbled on a problem using QButtonGroups. As far as I understand QButtonGroup logically organises AbstractButtons. A TAB would navigate on the group and the next TAB navigate away from the group. Within the group the AbstractButtons are navigated with the arrow keys. This all works fine with QRadioButtons. I need the same concept but with QCheckBoxes...but as soon as my ButtonGroup consists of CheckBoxes the arrow key navigation does't work any more (TAB navigation is as aspected). I also checked for checkable QPushButton - same problem here. For CheckBox and PushButton it is practically not possible to navigate through the Buttons of the group anymore (apart from the mouse).

Please consider the following code, change BUTTON_TYPE as neccessary:



#include <QApplication>
#include <QtGui>

#define BUTTON_TYPE 0

int main(int argc, char **args)
{
QApplication dialog(argc, args);

QWidget window;

QButtonGroup* bg = new QButtonGroup(&window);
QAbstractButton* cb1;
QAbstractButton* cb2;
QAbstractButton* cb3;

switch(BUTTON_TYPE)
{
case 0:

cb1 = new QCheckBox("cb1", &window);
cb2 = new QCheckBox("cb2", &window);
cb3 = new QCheckBox("cb3", &window);
break;
case 1:
cb1 = new QPushButton("cb1", &window);
cb2 = new QPushButton("cb2", &window);
cb3 = new QPushButton("cb3", &window);
cb1->setCheckable(true);
cb2->setCheckable(true);
cb3->setCheckable(true);
break;
default:
cb1 = new QRadioButton("cb1", &window);
cb2 = new QRadioButton("cb2", &window);
cb3 = new QRadioButton("cb3", &window);
break;
}

bg->addButton(cb1);
bg->addButton(cb2);
bg->addButton(cb3);

QLayout *layout = new QVBoxLayout;
layout->addWidget(cb1);
layout->addWidget(cb2);
layout->addWidget(cb3);

window.setLayout(layout);
window.show();

return dialog.exec();
}


I also flew over the Qt sourcecode (QAbstractButton::moveFocus), but on the rush I couldn't find any reason why it shouldn't work for the other two Button types (PushButton and Checkbox).
Did I forget something or is there any misconception on my behalf? Anybody else noticed this problem?

Hope you can enlighten me.

Regards

Santosh Reddy
3rd September 2013, 10:49
Set QAbstractButton::setAutoExclusive(true); on all the buttons/checkboxes

braindead
3rd September 2013, 11:03
Hello,

thanks for the hint. I thought it is enough that the ButtonGroup is Exclusive.
However, if it is so tight tied to AutoExlusive than that concept is not usable for me...i need just a logical group w/o AutoExclusive.
I have already implemented arrow key navigation between the group members, was just curious what the cause of the above stated problem is.

Nonetheless, I think it is not cleanly programmed in Qt source. You should never get stuck in a situation where you can't operate the UI anymore. But this is just my opinion.

Thanky again.

Santosh Reddy
3rd September 2013, 11:19
I see this as nice feature. Image a scenario where you have mulitple buttons in a group, and only few of them are exclusive.

Having exclusive setting on both group and button is more flexible solution.

braindead
3rd September 2013, 11:29
Yes, but those buttons that are not exclusive can never be reached using the keyboard...not with Arrow keys, not with TAB, not with any other key. So I am dependent on using the mouse. I think navigation should at least somehow work.

Santosh Reddy
3rd September 2013, 12:03
Yes I agree. Tab should work inside a QButtonGroup with non-exclusivve buttons. May be you can take this issue up with Qt Bugs.

braindead
3rd September 2013, 13:23
Santosh,

I played around a little bit with above sample code. Found out the following:

1) buttonGroup exclusive/ containing AbstractButtons autoexclusive: -> real autoexclusive behaviour, navigation per arrows possible

2) buttonGroup not exclusive/ containing AbstractButtons autoexclusive -> multiple checks allowed, navigation per arrows possible ( -> just what I need)

3) buttonGroup exclusive/ containing AbstractButtons not autoexclusive -> only check/uncheck of the first/focused button, no navigaton possible

4) buttonGroup not exclusive/ containing AbstractButtons not autoexclusive -> only check/uncheck of the first/focused button, no navigaton possible

Navigation seems only to be possible when the AbstractButtons are autoexclusive. However the autoexclusive behavriour is overruled by the exclusive property of the buttonGroup.

You are right this seems to be a case for Qt Bugs. And maybe the documentation could be a little bit more clear about the mutual influence.

Thanks