具有任意类型参数的 PyQt 信号 / PyQt_PyObject 相当于新型信号

发布于 2024-10-08 17:47:56 字数 354 浏览 1 评论 0原文

我有一个对象,它应该通过发出一个以新值作为参数的信号来表明值已更改。值的类型可以改变,所以我不确定如何编写信号类型。我知道我可以使用像这样的旧式信号来完成此操作:

self.emit(SIGNAL("valueChanged(PyQt_PyObject)"), newvalue)

但是我将如何使用新式信号来编写它?

我知道与此相关的之前的问题但那里没有给出“真正的”答案。

I have an object that should signal that a value has changed by emitting a signal with the new value as an argument. The type of the value can change, and so I'm unsure of how to write the signal type. I know that I can acconmplish this using old-style signals like this:

self.emit(SIGNAL("valueChanged(PyQt_PyObject)"), newvalue)

but how would I write this using new-style signals?

I am aware of a previous question related to this but no "real" answer was given there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2024-10-15 17:47:56

首先,您发出的对象需要将信号定义为其的属性:

class SomeClass(QObject):
    valueChanged = pyqtSignal(object)  

请注意,该信号有一个object类型的参数,该参数应该允许任何内容通过通过。然后,您应该能够使用任何数据类型的参数从类内发出信号:

self.valueChanged.emit(anyObject)

First, the object you're emitting from needs the signal defined as an attribute of its class:

class SomeClass(QObject):
    valueChanged = pyqtSignal(object)  

Notice the signal has one argument of type object, which should allow anything to pass through. Then, you should be able to emit the signal from within the class using an argument of any data type:

self.valueChanged.emit(anyObject)
话少心凉 2024-10-15 17:47:56

我是一个初学者,这是我试图回答的第一个问题,所以如果我误解了这个问题,请道歉...

以下代码发出一个发送自定义 Python 对象的信号,并且插槽使用该信号
类打印“Hello World”。

import sys
from PyQt4.QtCore import pyqtSignal, QObject

class NativePythonObject(object):
    def __init__(self, message):
        self.message = message

    def printMessage(self):
        print(self.message)
        sys.exit()

class SignalEmitter(QObject):
    theSignal = pyqtSignal(NativePythonObject)

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

    def emitSignal(self):
        self.theSignal.emit(toBeSent)

class ClassWithSlot(object):
    def __init__(self, signalEmitter):
        self.signalEmitter = signalEmitter
        self.signalEmitter.theSignal.connect(self.theSlot)

    def theSlot(self, ourNativePythonType):
        ourNativePythonType.printMessage()

if __name__ == "__main__":
    toBeSent = NativePythonObject("Hello World")
    signalEmitter = SignalEmitter(toBeSent)
    classWithSlot = ClassWithSlot(signalEmitter)
    signalEmitter.emitSignal()

I'm a beginner and this is the first question I'm attempting to answer, so apologies if I have misunderstood the question...

The following code emits a signal that sends a custom Python object, and the slot uses that
class to print "Hello World".

import sys
from PyQt4.QtCore import pyqtSignal, QObject

class NativePythonObject(object):
    def __init__(self, message):
        self.message = message

    def printMessage(self):
        print(self.message)
        sys.exit()

class SignalEmitter(QObject):
    theSignal = pyqtSignal(NativePythonObject)

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

    def emitSignal(self):
        self.theSignal.emit(toBeSent)

class ClassWithSlot(object):
    def __init__(self, signalEmitter):
        self.signalEmitter = signalEmitter
        self.signalEmitter.theSignal.connect(self.theSlot)

    def theSlot(self, ourNativePythonType):
        ourNativePythonType.printMessage()

if __name__ == "__main__":
    toBeSent = NativePythonObject("Hello World")
    signalEmitter = SignalEmitter(toBeSent)
    classWithSlot = ClassWithSlot(signalEmitter)
    signalEmitter.emitSignal()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文