PDA

View Full Version : Mutually Exclusive Checks in QTableWidget cells



davethomaspilot
9th August 2014, 13:56
Is there a way to define sets of checkable cells in a QTableWidget so that at most one cell can be checked?

anda_skoa
9th August 2014, 16:15
Without looking deeper into that I see two options:

1) connect to the table widget's itemChanged() signal and in the slot check if the item is checked and if it is uncheck all that are exclusive

2) derive from QTableWidgetItem, reimplement setData() to intercept Qt::CheckRoleState and do the unchecking in there (or delegate to a class that does it)

Cheers,
_

davethomaspilot
9th August 2014, 16:26
Ok, I'll do option 1.

Thanks,

Dave Thomas

davethomaspilot
10th August 2014, 01:45
#1 not so good, since the itemChanged() slot gets recursively called since the code is unchecked (changing) the item.

Ugh.

Added after 16 minutes:

So, in the slot I check a bool "unchecking". If set, just return.

Set the bool after the text and unset after unchecking all the exclusive items.

Not so bad, but easy to miss when you first implement.

Dave Thomas

anda_skoa
10th August 2014, 10:10
#1 not so good, since the itemChanged() slot gets recursively called since the code is unchecked (changing) the item.


Yes, that's why I wrote "check if the item is checked and if it is..."

If the signal is received for an item that is being unchecked, the condition is not true, the slot doesn't do anything.

Cheers,
_