简体   繁体   English

PyQt5 focusIN/Out 事件

[英]PyQt5 focusIN/Out events

I am using Python 3.4 and Qt 5 for the first time.我第一次使用 Python 3.4 和 Qt 5。 It's easy and I can understand most of functionality which I need.这很容易,我可以理解我需要的大部分功能。 But (there is always "but") I don't understand how to use focusOut / clearFocus / focusIn events.但是(总是有“但是”)我不明白如何使用focusOut / clearFocus / focusIn事件。

Am I right that old way:我是不是老样子:

QObject.connect(self.someWidget, QtCore.SIGNAL('focusOutEvent()'), self.myProcedure)

...does not work in Qt5? ...在 Qt5 中不起作用?

I tried to understand this unsuccessfully.我试图理解这一点,但没有成功。 I'll be very thankful for a short example how to catch an event when eg some of many QLineEdit has lost focus.我将非常感谢一个简短的例子,例如当许多QLineEdit中的一些失去焦点时如何捕捉事件。

The issue here is that focusInEvent / clearFocus / focusOutEvent are not signals, they are event handlers .这里的问题是focusInEvent / clearFocus / focusOutEvent不是信号,它们是事件处理程序 See for example here .参见此处的示例。 If you want to catch these events you will need to re-implement the event handler on your object, for example by subclassing QLineEdit.如果要捕获这些事件,则需要在对象上重新实现事件处理程序,例如通过子类化 QLineEdit。

class MyQLineEdit(QLineEdit):

    def focusInEvent(self, e):
        # Do something with the event here
        super(MyQLineEdit, self).focusInEvent(e) # Do the default action on the parent class QLineEdit

In PyQt5 the syntax for signals themselves is considerably simpler.在 PyQt5 中,信号本身的语法要简单得多。 Taking for example the textEdited signal from QLineEdit, you can use that as follows:以来自 QLineEdit 的textEdited信号为例,您可以按如下方式使用它:

self.someWidget.textEdited.connect( self.myProcedure )

This will connect your self.myProcedure function to the textEdited signal.这会将您的self.myProcedure函数连接到textEdited信号。 The target function will need to accept the event outputs, for example:目标函数需要接受事件输出,例如:

void    textEdited ( const QString & text )

So you could define your self.myProcedure in your class as follows and it will receive the QString sent by that event:所以你可以在你的类中定义你的self.myProcedure如下,它将接收由该事件发送的QString

def myProcedure(self, t):
    # Do something with the QString (text) object here

You can also define custom events as follows:您还可以按如下方式定义自定义事件:

from PyQt5.QtCore import QObject, pyqtSignal

class Foo(QObject):
    an_event = pyqtSignal()
    a_number = pyqtSignal(int)

In each of these cases pyqtSignal is used to define a property of the Foo class which you can connect to like any other signal.在每种情况下, pyqtSignal都用于定义Foo类的属性,您可以像任何其他信号一样连接到该属性。 So for example to handle the above we could create:例如,为了处理上述问题,我们可以创建:

def an_event_handler(self):
    # We receive nothing here

def a_number_handler(self, i):
    # We receive the int

You could then connect() and emit() the events as follows:然后,您可以按如下方式connect()emit()事件:

self.an_event.connect( self.an_event_handler )
self.a_number.connect( self.a_number_handler )

self.an_event.emit()   # Send the event and nothing else.
self.a_number.emit(1)  # Send the event an an int.

The link you posted gives more information on custom events , signal naming and overloading with the new syntax.您发布的链接提供了有关自定义事件、信号命名和使用新语法重载的更多信息

It took me a while to get this figured out so I thought I would post it here to help anyone else having this problem.我花了一段时间才弄明白这个问题,所以我想我会在这里发布它以帮助其他遇到此问题的人。 It is in Python 3.9.6 and PyQt 6.1.2.它在 Python 3.9.6 和 PyQt 6.1.2 中。

This changes the color of the widget when it is in focus and again when it is out of focus.这会在小部件聚焦时更改小部件的颜色,并在失焦时再次更改。

import sys
from PyQt6 import QtWidgets


class Main(QtWidgets.QWidget):

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

        self.setWindowTitle("Qt Testing")
        self.setGeometry(0, 0, 640, 120)

        h = QtWidgets.QHBoxLayout()

        w = ColorQLineEdit("one")
        h.addWidget(w)

        w = ColorQLineEdit("two")
        h.addWidget(w)

        self.setLayout(h)


class ColorQLineEdit(QtWidgets.QLineEdit):

    def focusInEvent(self, event):
        print("in")
        self.setStyleSheet("background-color: yellow; color: red;")
        super().focusInEvent(event)

    def focusOutEvent(self, event):
        print("out")
        self.setStyleSheet("background-color: white; color: black;")
        super().focusOutEvent(event)


app = QtWidgets.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM