如何拦截 QComboBox 中的鼠标信号

How to intercept a mouse signal in a QComboBox

我在 QDialog 小部件上放置了一个自定义组合框,但我无法捕捉到任何鼠标信号。我子 classed QComboBox 以拦截 QComboBox 未提供的两个信号:LostFocusEvent 和 mouseDobleClickEvent。 LostFocusEvent 运行良好,但组合没有触发鼠标事件。我需要组合框上的三个信号,但只提供了一个合适的信号。

我试图设置 combo.grabMouse(),无视文档警告,combo.doubleClicked 开始工作,但通过信号连接的所有其他小部件开始表现不稳定。 还尝试了 combo.view().doubleClick.connect,结果相似。我还尝试了具有类似结果的其他鼠标事件(新闻发布等) 最后,我尝试在 comboBox sub class 中使用事件而不是 QMouseEvent,但它被 focusOutEvent 槽拦截了。 鼠标事件在 QPushButtons 上工作,包括双击 QTableView 小部件 使用 Windows 8 Python 3.7 PyQt5.

`class Agreement(QDialog):
    def __init__(self,db, address, parent=None):
        super().__init__(parent= None)
        self.parent = parent
.......................................

    def setUi(self):
    .....................................
        self.comboSupplier = ComboFocus.FocusCombo(self)
        self.comboSupplier.setMaximumSize(220,30)
        self.comboSupplier.setEditable(True)
        #self.comboSupplier.grabMouse()
        self.comboSupplier.activated.connect(self.supplierChange)
        self.comboSupplier.focusLost.connect(self.supplierFocusLost)
        self.comboSupplier.doubleClicked.connect(self.editContact)
    ...........................................

     def supplierChange(self):
        try:
            row = self.comboSupplier.currentIndex()
            idx = self.comboSupplier.model().index(row,0)
            self.supplierId = self.comboSupplier.model().data(idx)
            self.agreementTitle[0] = self.comboSupplier.currentText()
            self.setAgreementTitle()
            self.okToSave[2] = int(self.supplierId)
            self.okSaving()
        except TypeError as err:
            print('supplierChange' + type(err).__name__ + ' ' + err.args[0])

    @pyqtSlot()
    def editContact(self):
        try:
            c = Contacts(self.db,self.comboSupplier.currentText(), 
                APM.OPEN_EDIT_ONE, self.supplierId,parent=self)
            c.show()
            c.exec()
        except Exception as err:
            print(type(err).__name__, err-args)

    @pyqtSlot(ComboFocus.FocusCombo)
    def supplierFocusLost(self, combo):
        try:
            self.setFocusPolicy(Qt.NoFocus)
            name = combo.currentText()
            if combo.findText(name) > -1:
                return
       ........................................

class FocusCombo(QComboBox):
    focusLost = pyqtSignal(QComboBox)
    focusGot = pyqtSignal(QComboBox)
    doubleClicked = pyqtSignal(QComboBox)

    def __init__(self, parent = None):
        super().__init__(parent)
        self.parent = parent

    def mouseDoubleClickEvent(self,event=QMouseEvent.MouseButtonDblClick):
        print("double click detected")

        self.doubleClicked.emit(self)

    def focusOutEvent(self, event):
        if event.gotFocus():
            self.focusGot.emit(self)

        elif event.lostFocus():
            self.focusLost.emit(self)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    cb = FocusCombo()
    cb.show()
    app.exec_()
    sys.exit(app.exec_())

我想双击组合框打开一个小部件,以即时编辑联系人属性。

当您将 QLineEdit 设置为可编辑时,会添加一个 QLineEdit,因此要跟踪您的事件,您必须使用 eventFilter:

from PyQt5 import QtCore, QtGui, QtWidgets

class FocusCombo(QtWidgets.QComboBox):
    focusLost = QtCore.pyqtSignal(QtWidgets.QComboBox)
    focusGot = QtCore.pyqtSignal(QtWidgets.QComboBox)
    doubleClicked = QtCore.pyqtSignal(QtWidgets.QComboBox)

    def setEditable(self, editable):
        super(FocusCombo, self).setEditable(editable)
        if self.lineEdit() is not None:
            self.lineEdit().installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj is self.lineEdit():
            if event.type() == QtCore.QEvent.MouseButtonDblClick:
                self.doubleClicked.emit(self)
            """elif event.type() == QtCore.QEvent.MouseButtonPress:
                print("press")
            elif event.type() == QtCore.QEvent.MouseButtonRelease:
                print("release")"""
        return super(FocusCombo, self).eventFilter(obj, event)

    def mouseDoubleClickEvent(self,event):
        print("double click detected")
        self.doubleClicked.emit(self)
        super(FocusCombo, self).mouseDoubleClickEvent(event)

    def focusOutEvent(self, event):
        if event.gotFocus():
            self.focusGot.emit(self)

        elif event.lostFocus():
            self.focusLost.emit(self)
        super(FocusCombo, self).focusOutEvent(event)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    cb = FocusCombo()
    cb.addItems(list("abcdef"))
    cb.setEditable(True)
    cb.doubleClicked.connect(print)
    cb.show()
    sys.exit(app.exec_())