If you want to activate a flag of an item you must use the operator |
:
it.setFlags(it.flags() | Qt.ItemIsSelectable)
If you want to deactivate it you must use & ~
:
it.setFlags(it.flags() & ~Qt.ItemIsSelectable)
Example:
import sys
from PySide.QtGui import QApplication, QWidget, QVBoxLayout, QListWidget, QPushButton
from PySide.QtCore import Qt
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
lay = QVBoxLayout(self)
self.button = QPushButton("UnSelectable")
self.listWidget = QListWidget()
for i in range(10):
self.listWidget.addItem("item {}".format(i))
lay.addWidget(self.button)
lay.addWidget(self.listWidget)
self.button.clicked.connect(self.on_clicked)
def on_clicked(self):
if self.button.text() == "UnSelectable":
self.button.setText("Selectable")
for i in range(self.listWidget.count()):
it = self.listWidget.item(i)
it.setFlags(it.flags() & ~Qt.ItemIsSelectable)
else:
self.button.setText("UnSelectable")
for i in range(self.listWidget.count()):
it = self.listWidget.item(i)
it.setFlags(it.flags() | Qt.ItemIsSelectable)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())