PDA

View Full Version : Using QButtonGroup in Designer - Ways to set ID?



zcybercomputing
26th April 2013, 04:29
Hi,

I have done some searching around on the forums about how to use QButtonGroup and found many of the posts are old, and took a bit to translate in to the latest version. I have things working partially now, but I can't figure out if there is a way to make it assign the ID in a reasonable manner.

Here is my setup. I have 20 buttons in a 4 by 5 grid. This was created using Designer in Qt Creator.

Note: to create the QButtonGroup by right clicking only works when you right click in the Object Inspector, and is not an option if right clicking on the selected buttons themselves. Other forum posts did not say this so it took a while for me to figure that out.

I placed a connect statement in the constructor and was able to get the index output as a qDebug in the destination function. This is where the weirdness starts. I am aware that the auto index uses negative numbers and that is not a problem, but it seems to start from right to left instead of left to right. My index output looks like this. (organised in the arrangement of the buttons)



-5 -4 -3 -2
-9 -8 -7 -6
-13 -12 -11 -10
-17 -16 -15 -14
-21 -20 -19 -18


How can I change this behavior in Designer? The default tab order had the same numbers, but after changing that in Designer it didn't effect the numbering order. I have multiple sets of these button grids so I am trying to avoid having to call setId for each button separately.

I want to trigger a process_buttons(int index) function with the index going left to right, top to bottom. My process_buttons(int) function needs the index in the following mapping. I'm not sure if I need zero indexing, but subtracting a constant isn't a big issue.



1 2 3 4
6 7 8 9
10 11 12 13
14 15 16 17
18 19 20 21

ChrisW67
26th April 2013, 05:07
Note: to create the QButtonGroup by right clicking only works when you right click in the Object Inspector, and is not an option if right clicking on the selected buttons themselves. Other forum posts did not say this so it took a while for me to figure that out.

Right-clicking on a single button does not allow you to create a button group, but if one already exists you get group related options. Right-clicking with multiple selected buttons will allow you to create a new button group.

AFAICT there's no way to explicitly set the id of a widget in the button group in Designer. Automatic IDs are dependent on the order of their adding to the generated QButtonGroup, which with uic generated code appears to be the order of addition of the button to the Designer form. I've not seen that documented so I would not rely on it.

In your form constructor I would simply call QButtonGroup::setId() for each button and set the ID you want. If you adopt consistent button naming then you might be able to reduce this to a loop over the buttons in the group, extracting the id from the name for example.

anda_skoa
26th April 2013, 11:03
Since your grid seems to define the button ids, you could iterate over it line by line, each line in the direction you want and just count the ID value up as you go.

Cheers,
_

zcybercomputing
29th April 2013, 17:21
I am not referring to right clicking a single button. When I select multiple buttons in Designer and right click on the selected buttons it seems to interpret that as a right click on a single button instead of the selection. Anyway that is just a side annoyance.

My buttons are named in the following manner.

group_aux1
aux1_Button1
aux1_Button2
etc...

Any tips on how to do a for loop over these so I can call setID() for each without tons of lines of code? I haven't done that type of thing before so please excuse my ignorance. I would appreciate at least a some links to the appropriate docs so I can learn faster.

ChrisW67
30th April 2013, 05:27
Each button is a QObject and has an objectName(), and Designer sets the objectName() to match the name you see on the Object Inspector panel. You can iterate over QPushButton children of the form (or some child widget containing the buttons) where the name matches a pattern, extract the numeric part and use that as the ID. Something like:


ui->setupUi(this);

const QRegExp re("aux1_button(\\d+)");
foreach(QPushButton *button, findChildren<QPushButton*>(re)) {
(void) re.indexIn(button->objectName()); // we know it matches, but we need the captured text
const int id = re.cap(1).toInt();
ui->buttonGroup->setId(button, id); // assuming the button is already in the button group
}