undo a setFlags ItemIsUserCheckable
checkbox, python, qt
Solution
To Remove a flag try:
item.setFlags(item.flags() ^ QtCore.Qt.ItemIsUserCheckable)
^^ `item` is the element you want to unFlag. In simple terms `^` operator works as the opposite to `|` and thus excludes the `QtCore.Qt.ItemIsUserCheckable` flag from the existing flags that `item.flags()` returns
Update:
While the above code will disable the checkable feature, to completely hide the checkbox try
item.setData(Qt::CheckStateRole, QVariant());
Do note that this does not preserve the state of the checkbox after this call is made.
Problem
I created a QListWidget and I want to make its elements checkable only at particular places in the application (I need it for a particular application where the user select a principal element on the list while clicking on it and then select secondary elements while checking their boxes). Im ok with making the elements checkable but I can't find a solution to undo this and remove the checkboxes... I did this : ``` for i in range(self.listWidget.count()): item=self.listWidget.item(i) item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled) item.setCheckState(QtCore.Qt.Unchecked) ``` but then I struggle... I tried : ``` tmp=self.listWidget.takeItem(i) self.listWidget.addItem(tmp) ``` But the item come back with its checkbox :( I could simply destroy the item and then put it back again but this can be a kind of heavy treatment ! Is there any solution ? Thanks !