PyQt4中组合框的下拉事件/回调

2 投票
2 回答
3053 浏览
提问于 2025-04-16 23:21

在pyqt4的下拉框(组合框)中,有没有类似的回调或者事件?就像这样:self.connect(self.ui.combobox,SIGNAL("activated(int)"),self.refresh

2 个回答

0

也许你可以试试

customContextMenuRequested(const QPoint &pos) 

信号(这是从QWidget继承来的)?

2

QCombobox这个控件默认是用QListView来显示下拉菜单里的选项。你可以通过view()这个属性来访问它。我不知道有没有专门的信号可以用来处理这个。

不过,你可以设置一个事件过滤器来实现你想要的功能。具体做法是使用installEventFilter方法在下拉框的视图上,然后实现eventFilter这个方法:

from PyQt4 import QtCore, QtGui
class ShowEventFilter(QtCore.QObject):
    def eventFilter(self, filteredObj, event):
        if event.type() == QtCore.QEvent.Show:
            print "Popup Showed !"
            # do whatever you want
        return QtCore.QObject.eventFilter(self, filteredObj, event)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    cb = QtGui.QComboBox()
    cb.addItems(['a', 'b', 'c'])

    eventFilter = ShowEventFilter()
    cb.view().installEventFilter(eventFilter)
    cb.show()
    app.exec_()

撰写回答