Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch mouse button pressed signal from qComboBox popup menu

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?

enter image description here

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
like image 740
Aleksandar Avatar asked Jan 17 '14 13:01

Aleksandar


1 Answers

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)
like image 163
ekhumoro Avatar answered Sep 30 '22 06:09

ekhumoro