How-to detect any (from 160) QCheckBox stateChanged
Hello,
i have application with 160 QCheckBoxes. Is there a way to detect if any of 160 checkboxes has changed their state? I mean, is there a way to use only one connect() to detect stateChanged for any of 160 boxes? I don't want to add 160 connect() lines. Or are there any other options, other than connect()?
All checkboxes are added / created in QT Designer.
I need to detect when specific checkbox is selected, so i can disable opposite checkbox. To prevent user from accidentally selecting opposite checkboxes.
Re: How-to detect any (from 160) QCheckBox stateChanged
Quote:
I need to detect when specific checkbox is selected, so i can disable opposite checkbox. To prevent user from accidentally selecting opposite checkboxes.
This sounds like you should be using QRadioButton instead of QCheckBox and put the pairs of buttons into exclusive QButtonGroup. You can design the UI in Qt Designer, but the buttons have to be added to the QButtonGroup using code.
If you have to use QCheckBox, then you can connect all of them to the same slot. Inside the slot, you can determine which check box was changed by using QObject::sender() and doing a qobject_cast< QCheckBox * > to retrieve the actual QCheckBox instance.
To make it easy to determine the matching pairs of check boxes, I would make a QMap< QCheckBox *, QCheckBox * > with all of the pairs added. Once you have the pointer to the check box that was clicked, you can look up the opposite box using the map.
You cannot avoid writing the code to connect the check boxes to the slot or to build the lookup map. I think it is a bad idea to make signal-slot connections in Qt Designer, because then those connections are hidden away in the UI file where you can forget them. When I was first learning Qt, I would do this, and it caused a large number of bugs and difficulty in debugging them because I could not determine why a slot was being called (or called twice) because a connection was hidden inside the UI code.
1 Attachment(s)
Re: How-to detect any (from 160) QCheckBox stateChanged
Thank you for your reply d_stranz.
On image you can see how checkboxes are setup. They are not exactly in "pair", that's why checkboxes and not radiobuttons.
Here are examples, how it should work:
If you select "?len" top left 6, it should disable all other checkboxes in top left 6 "column".
If you select "konus" top left 6, it should disable all other checkboxes in top left 6 "column".
If you select "prevleka" OR "prevleka" and "anker" top left 6, it should disable "?len" AND "konus" top left 6.
Attachment 13749
Quote:
Originally Posted by d_stranz
...
You cannot avoid writing the code to connect the check boxes to the slot or to build the lookup map. I think it is a bad idea to make signal-slot connections in Qt Designer, because then those connections are hidden away in the UI file where you can forget them. When I was first learning Qt, I would do this, and it caused a large number of bugs and difficulty in debugging them because I could not determine why a slot was being called (or called twice) because a connection was hidden inside the UI code.
Regarding writing the code to connect all checkboxes. I would definitely prefer to do it with code, rather than in QT Designer. I just wanted to see if it was possible to avoid 160 lines of connect(). Sort of like connect() with a "wildcard" selector.
Re: How-to detect any (from 160) QCheckBox stateChanged
Quote:
Sort of like connect() with a "wildcard" selector.
That is not possible as far as I know.
Quote:
I just wanted to see if it was possible to avoid 160 lines of connect().
If you use Qt Designer to create the layout and add the labels and lines but not the check boxes, you can create the check boxes in code, place them in the layout, and add them to a vector of QCheckBox *. Then you can use a for() loop to connect each check box to the slot in a single statement.
Quote:
Here are examples, how it should work:
That sounds very complicated. Maybe you should look at QStateMachine and develop a set of transitions that implement your enable / disable logic.