如何使用自定义 python 类型参数发出 PySide 信号? [英] How do I emit a PySide signal with a custom python type argument?

查看:121
本文介绍了如何使用自定义 python 类型参数发出 PySide 信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PySide python Qt 程序中无法正确使用信号.我想发出一个信号,该信号采用自定义 python 类型的单个参数.文档

I am having trouble correctly using signals in my PySide python Qt program. I want to emit a signal that takes a single argument of a custom python type. The documentation says

可以使用 QtCore.signal() 类定义信号.Python 类型和 C 类型可以作为参数传递给它.

Signals can be defined using the QtCore.signal() class. Python types and C types can be passed as parameters to it.

所以我尝试了以下方法:

So I tried the following:

from PySide import QtCore
from PySide.QtCore import QObject

class Foo:
    pass

class Bar(QObject):
    sig = QtCore.Signal(Foo)

    def baz(self):
        foo = Foo()
        self.sig.emit(foo)

bar = Bar()
bar.baz()

但得到以下错误:

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    bar.baz()
  File "test.py", line 12, in baz
    self.sig.emit(foo)
TypeError: sig() only accepts 0 arguments, 1 given!

如果我从 QObject 派生 Foo 类,则程序运行不会出错.但是我不应该能够将我的自定义类型作为参数传递给信号,即使该类型不是从 QObject 派生的?

If instead I derive the Foo class from QObject, the program runs without error. But shouldn't I be able to pass my custom type as an argument to the signal, even if that type does not derive from QObject?

这是在 Windows 7 上使用 python 2.7.2 和 PySide 1.0.4.

This is with python 2.7.2 and PySide 1.0.4 on Windows 7.

推荐答案

您创建了一个 "旧式类",显然不支持作为信号参数类型.

You created an "old-style class", which apparently isn't supported as a signal parameter type.

该类应该从另一个新式类继承,或者从基本的object类型继承:

The class should inherit from another new-style class, or from the base object type:

class Foo(object):
    pass

这篇关于如何使用自定义 python 类型参数发出 PySide 信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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