0

For the life of me I cant figure this out. I have a table set up, and in row 0 col 0 I have a checkbox. If that checkbox is not clicked I want to disable row 1, col 0. Otherwise if clicked, enable row 1 col 0.

To disable it I've been using flags:

if not self.example_table.cellWidget(0,0).isChecked():
    flags = QtCore.Qt.ItemFlags()
    flags != QtCore.Qt.ItemIsEnabled
    # flags != QtCore.Qt.ItemIsEditable (This also works to disable it)
    self.example_table.item(1,0).setFlags(flags)

The problem is that when I try and make an else with the inverse, it will not re-enable the cell. Here is what I've tried.

else:
    flags = QtCore.Qt.ItemFlags()
    flags == QtCore.Qt.ItemIsEnabled #(still doesnt work)
    # flags = QtCore.Qt.ItemIsEnabled (didnt work)
    # flags == QtCore.Qt.ItemIsEditable (didnt work)
    # flags = QtCore.Qt.ItemIsEditable (didnt work)

    self.tblActivityInfo.item(1, 0).setFlags(flags)

No matter what I try, the cell at 1,0 remains disabled.

self.example_table is just a QtWidgets.QTableWidget(self.centralwidget)

Cole
  • 116
  • 7

1 Answers1

1

What you're doing doesn't make much sense: both the second flags lines are just comparisons, they do absolutely nothing.

You should get the current flags for the existing item instead, and then use binary operators to update them.

item = self.example_table.item(1, 0)
flags = item.flags()
if not self.example_table.cellWidget(0,0).isChecked():
    # set the ItemIsEnabled flag bit to 0
    flags &= ~QtCore.Qt.ItemIsEditable
else:
    # set the ItemIsEnabled flag bit to 1
    flags |= QtCore.Qt.ItemIsEditable
    flags |= QtCore.Qt.ItemIsEnabled

item.setFlags(flags)

I recommend you to better study what comparison operators do and how binary bitwise operations work.

Cole
  • 116
  • 7
musicamante
  • 41,230
  • 6
  • 33
  • 58
  • So for whatever reason, this works to re-enable the cell, however I can not change the value there. I changed the 'ItemIsEnabled' to 'ItemIsEditable'. This did not re-enable the cell, however, so I added 'flags |= QtCore.Qt.ItemIsEnabled' after. This works, thank you! – Cole Jun 02 '20 at 19:31
  • Yeah I too dont know why they were comparisons, but thats the code snipets I found on many, many different google searches. I also don't understand why the comparisons would make changes to the affect of the flags, but they did. – Cole Jun 02 '20 at 19:32
  • 1
    No, they didn't. It "worked" in the first case (disabling the item) because you created an "empty" flag (as in 0 value), which doesn't enable any flags (including, obviously, ItemIsEnabled). – musicamante Jun 02 '20 at 19:37
  • That makes more sense. I feel like someone once answered the question with that, and it was copied over multiple times to different responses cause it "worked" to disable it. – Cole Jun 02 '20 at 19:47
  • 1
    That's very likely, unfortunately it's common to use code (especially here on SO) without actually understanding what it does. Anyway, I've seen your edit suggestion, and I don't understand it: `ItemIsEnabled` and `ItemIsEditable` are two very distinct flags, and they exist for a specific reason. Do you want to (dis|en)able the item or make it [un]editable? – musicamante Jun 02 '20 at 19:51
  • You're right they are two different flags. For some reason, when I use just ItemIsEnabled, as in your original, it will disable the cell, then re-enable it. However, even though re-enabled, it is not editable for some reason. When just using ItemIsEditable, it will disable the cell (and look identical to a 'not' ItemIsEnabled cell. However, when I try to re-enable the cell using ItemIsEditable, it will still be grayed out and unclickable. Using the combination of the two calls worked though. – Cole Jun 02 '20 at 20:03