带有任意类型参数的 PyQt 信号/PyQt_PyObject 等效于新式信号 [英] PyQt signal with arguments of arbitrary type / PyQt_PyObject equivalent for new-style signals

查看:33
本文介绍了带有任意类型参数的 PyQt 信号/PyQt_PyObject 等效于新式信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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

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

注意信号有一个 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)

这篇关于带有任意类型参数的 PyQt 信号/PyQt_PyObject 等效于新式信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆