Search code examples
pythonpyqtpyqt4qcombobox

QComboBox mouse press event when combo box is initially pressed PyQt4


I'm trying to schedule a mouse press event for a QComboBox. I was wondering if there is any way to schedule a mouse press event on the initial QComboBox click -- the click that brings up the list of items to select. I've already used the currentIndexChanged(int) signal to call a function once the user selects one of the items from the drop down menu, but I'm trying to refresh my QComboBox list with new entries once the user clicks on it. (I have a feeling that this approach may be misguided, but I guess that's another question.)

I've tried making a QComboBox subclass with def mousePressEvent(self, e), but it doesn't seem to do anything. I've also tried def mousePressEvent(self, e) in the QtGui.QWidget class that holds my QComboClass object but, unsurprisingly, that only captures mouse presses for the QtGui.QWidget.


Solution

  • Your current approach is misguided. Even if you could get it working, it would fail whenever the list was opened via the keyboard.

    The correct way to do this is to override showPopup:

    class ComboBox(QtGui.QComboBox):
        def showPopup(self):
            self.insertItem(0, 'Added')
            super(ComboBox, self).showPopup()