当qpushbutton聚焦时触发python方法

时间:2019-04-17 06:14:42

标签: python pyqt pyqt5

我的pyqt5 gui中有3个qPushButton。我想通过按Tab键选择不同的qPushButton时触发不同的方法。 这意味着当我按下Tab键一次(然后下一个qpushbutton(例如qpushbutton2)被聚焦)时,我想运行特定于qpushbutton2的功能。 有人可以帮我找到一种方法吗?

1 个答案:

答案 0 :(得分:3)

想法是捕获focusIn事件,因此至少有可能的方法:

1。覆盖QPushButton的focusInEvent方法

from PyQt5 import QtCore, QtWidgets


class PushButton(QtWidgets.QPushButton):
    focusSignal = QtCore.pyqtSignal()

    def focusInEvent(self, event):
        self.focusSignal.emit()
        super(PushButton, self).focusInEvent(event)


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        button1 = PushButton("button1")
        button2 = PushButton("button2")
        button3 = PushButton("button3")
        lay = QtWidgets.QVBoxLayout(self)
        for button in (button1, button2, button3):
            lay.addWidget(button)
        button1.focusSignal.connect(self.focus_button1)
        button2.focusSignal.connect(self.focus_button2)
        button3.focusSignal.connect(self.focus_button3)

    @QtCore.pyqtSlot()
    def focus_button1(self):
        print("button1")

    @QtCore.pyqtSlot()
    def focus_button2(self):
        print("button2")

    @QtCore.pyqtSlot()
    def focus_button3(self):
        print("button3")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

2。使用eventFilter

from PyQt5 import QtCore, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.button1 = QtWidgets.QPushButton("button1")
        self.button2 = QtWidgets.QPushButton("button2")
        self.button3 = QtWidgets.QPushButton("button3")
        lay = QtWidgets.QVBoxLayout(self)
        for button in (self.button1, self.button2, self.button3):
            lay.addWidget(button)
            button.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.FocusIn:
            if self.button1 is obj:
                self.focus_button1()
            elif self.button2 is obj:
                self.focus_button2()
            elif self.button3 is obj:
                self.focus_button3()
        return super(Widget, self).eventFilter(obj, event)

    def focus_button1(self):
        print("button1")

    def focus_button2(self):
        print("button2")

    def focus_button3(self):
        print("button3")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

3。 QApplication的focusChanged信号

from PyQt5 import QtCore, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        self.button1 = QtWidgets.QPushButton("button1")
        self.button2 = QtWidgets.QPushButton("button2")
        self.button3 = QtWidgets.QPushButton("button3")
        lay = QtWidgets.QVBoxLayout(self)
        for button in (self.button1, self.button2, self.button3):
            lay.addWidget(button)

        QtWidgets.qApp.focusChanged.connect(self.on_focusChanged)

    @QtCore.pyqtSlot("QWidget*", "QWidget*")
    def on_focusChanged(self, old, now):
        if now == self.button1:
            self.focus_button1()
        elif now == self.button2:
            self.focus_button2()
        elif now == self.button3:
            self.focus_button3()

    def focus_button1(self):
        print("button1")

    def focus_button2(self):
        print("button2")

    def focus_button3(self):
        print("button3")


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())