QComboBox click event

2020-03-23 19:02发布

问题:

I have been trying to get a QComboBox in PyQt5 to become populated from a database table. The problem is trying to find a method that recognizes a click event on it.

In my GUI, my combo-box is initially empty, but upon clicking on it I wish for the click event to activate my method for communicating to the database and populating the drop-down list. It seems so far that there is no built-in event handler for a click-event for the combo-box. I am hoping that I am wrong on this. I hope someone will be able to tell me that there is a way to do this.

The best article I could find on my use-case here is from this link referring to PyQt4 QComboBox:

  • dropdown event/callback in combo-box in pyqt4

I also found another link that contains a nice image of a QComboBox. The first element seems to be a label followed by a list:

  • Catch mouse button pressed signal from QComboBox popup menu

回答1:

You can override the showPopup method to achieve this, which will work no matter how the drop-down list is opened (i.e. via the mouse, keyboard, or shortcuts):

from PyQt5 import QtCore, QtWidgets

class ComboBox(QtWidgets.QComboBox):
    popupAboutToBeShown = QtCore.pyqtSignal()

    def showPopup(self):
        self.popupAboutToBeShown.emit()
        super(ComboBox, self).showPopup()

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.combo = ComboBox(self)
        self.combo.popupAboutToBeShown.connect(self.populateConbo)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.combo)

    def populateConbo(self):
        if not self.combo.count():
            self.combo.addItems('One Two Three Four'.split())

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

However, for your particular use-case, I think a better solution might be to set a QSqlQueryModel on the combo-box, so that the items are updated from the database automatically.



回答2:

Alternative Solution I :

We can use frame click, the code is to be used in the container of the combo box (windows/dialog/etc.)

def mousePressEvent(self, event):
        print("Hello world !")

or

def mousePressEvent():
        print("Hello world !")

Alternative Solution II :

We could connect a handler to the pressed signal of the combo's view

    self.uiComboBox.view().pressed.connect(self.handleItemPressed)
    ...

    def handleItemPressed(self, index):
        item = self.uiComboBox.model().itemFromIndex(index)
        print("Do something with the selected item")


回答3:

Why would you want to populate it when it's activated rather than when the window is loaded?

I am currently developing an application with PySide (another Python binding for the Qt framework), and I populate my comboboxes in the mainwindow class __init__ function, which seems to be the way to go, judging by many examples.

Look at the example code under "QCombobox" over at Zetcode.