Вопрос

Hi I am trying to use a QComboBox to populate a list of databases that can be found on a server.

Is there any signal on a QComboBox that can be used to connect it to a method that can populate the QComboBox when the user clicks on it.

I am trying to create a Dialog that is used the way that users normally create a database connection in the Windows environment, allowing them to specify the server name, user name and password and then when they click on the "Select Database" combo box the list of databases are retrieved from the server and they can select one from the list.

Would reimplementing the mousePressEvent in a custom Class inheriting from QComboBox be what is required to gain this functionality?

Это было полезно?

Решение

You can use a eventfilter to catch the mousepress event.

Minimal example:

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()


        self.combo = QtGui.QComboBox(self)
        self.combo.installEventFilter(self)
        self.show()

    def eventFilter(self,target,event):
        if target == self.combo and event.type() == QtCore.QEvent.MouseButtonPress:
            print "Button press"
            self.fillComboBox()

        return False

    def fillComboBox(self):
        self.combo.addItem('foo')
        self.combo.addItem('bar')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top