How do I emit a PySide signal with a custom python type argument?

一世执手 提交于 2019-12-06 05:31:31

问题


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

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()

But get the following error:

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!

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?

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


回答1:


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

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

class Foo(object):
    pass


来源:https://stackoverflow.com/questions/11277068/how-do-i-emit-a-pyside-signal-with-a-custom-python-type-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!