如何在QComboBox中拦截鼠标信号

时间:2019-05-02 16:00:50

标签: python python-3.x pyqt pyqt5 qcombobox

我在QDialog小部件上放置了一个自定义组合框,但是我无法捕获任何鼠标信号。我将QComboBox划分为子类,以拦截QComboBox没有提供的两个信号:LostFocusEvent和mouseDobleClickEvent。 LostFocusEvent效果很好,但是该组合没有触发鼠标事件。我在组合框上需要三个信号,但只提供了一个合适的信号。

我试图设置combo.grabMouse(),而无视文档警告,并且combo.doubleClicked开始工作,但是通过信号连接的所有其他小部件开始行为异常。 还尝试了combo.view()。doubleClick.connect,结果类似。我也尝试了其他鼠标事件,但结果相似(Press- Release-等) 最后,我尝试在comboBox子类中使用事件而不是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_())

我想双击comboBox来打开一个小部件,以便即时编辑联系人属性。

1 个答案:

答案 0 :(得分:0)

将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_())