I have made multi-select QComboBox
. Items are checkable (every item have check box and text value). CheckBox is checked only when user click on it. What I want is to catch signal when user click on text value so I can set check box next to it checked. How to do that?
This is how I set model to combo box:
areas = ["Area one", "Area two", "Area three", "Area four"]
model = QtGui.QStandardItemModel(5, 1)# 5 rows, 1 col
firstItem = QtGui.QStandardItem("---- Select area(s) ----")
firstItem.setBackground(QtGui.QBrush(QtGui.QColor(200, 200, 200)))
firstItem.setSelectable(False)
model.setItem(0, 0, firstItem)
for i,area in enumerate(areas):
item = QtGui.QStandardItem(area)
item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
item.setData(QtCore.Qt.Unchecked, QtCore.Qt.CheckStateRole)
model.setItem(i+1, 0, item)
self.ui.comboBox.setModel(model)
self.ui.comboBox.installEventFilter(self)
I've added event filter to block wheel event,so firstItem
's text is always shown as "chosen"
def eventFilter(self,target,event):
if target == self.ui.comboBox:
if(event.type()== QtCore.QEvent.Wheel):
#wheel event is blocked here
return True
return False
You need to connect a handler to the pressed signal of the combo's view:
self.ui.comboBox.view().pressed.connect(self.handleItemPressed)
...
def handleItemPressed(self, index):
item = self.ui.comboBox.model().itemFromIndex(index)
if item.checkState() == QtCore.Qt.Checked:
item.setCheckState(QtCore.Qt.Unchecked)
else:
item.setCheckState(QtCore.Qt.Checked)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With