-2

I have a QTableWidget and want items to be selectable depending on what's already selected (only if they have the same content in second column).

I know how to make items selectable or not. But everything should remain selectable so the user can select a single item to start the selection fresh. Only adding to a selection by keeping shift or ctrl pressed should allow/select only suitable items.

Where can I hook into to make adding to a selection only accept suitable rows?

Note: I'm using PySide but I can work from C++ or PyQt code just fine.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
  • Maybe you can do it subclassing QItemSelectionModel – Fabio Mar 24 '16 at 13:08
  • And then? Overloading select might get me half way but ideally I don't even want items show up selectable when the mouse hovers over them when shift is ctrl is pressed. Altering the ItemIsSelectable flag every time cttrl/shift is pressed seems bad. – Goswin von Brederlow Mar 24 '16 at 13:21
  • 1
    What do you mean with "items show up selectable when the mouse hovers over them"? Does not seem to me that there is any difference beetween selectable items and not selectable items when the mouse hovers over them. I think you can overload the two `select` methods of `QItemSelectionModel` and select the items according with the already selected items (but not sure, I've never done anything like this) – Fabio Mar 24 '16 at 14:39
  • You are right, selectable and unselectable items look the same with the mouse over them. But enabled icons get a light blue shade and not enabled items a light grey shade. So I ment altering the ItemIsEnabled flag instead of ItemIsSelectable. – Goswin von Brederlow Mar 31 '16 at 11:06

1 Answers1

-1

You could connect to the QTableWidget.itemSelectionChanged signal and modify the Qt.ItemIsSelectable flags of all the other items in the table based off the selection.

from itertools import product

table = QtGui.QTableWidget()
table.itemSelectionChanged.connect(self.on_itemSelectionChanged)

@QtCore.pyqtSlot()
def on_itemSelectionChanged(self):
    sel_items = self.table.selectedItems()

    # get all items in table
    items = []
    for r, c in product(range(self.table.rowCount()), range(self.table.columnCount())):
        items.append(self.table.item(r, c)

    # Loop through all the items in the table and
    # set the selection flag based of already selected items.
    for item in items:
        if can_be_selected:
            item.setFlags(item.flags() | QtCore.Qt.ItemIsSelectable)
        else:
            item.setFlags(item.flags() & ~QtCore.Qt.ItemIsSelectable)
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • Then you can't select (without shift or ctrl) a new item to replace the current selection. The "can_be_selected" is dependent on weather the current selection is replaced or extended. – Goswin von Brederlow Mar 31 '16 at 11:09
  • You would just have to de-select your current selection before making a new selection. – Brendan Abel Mar 31 '16 at 16:09