How do I emit a PySide signal with a custom python

2020-07-17 16:15发布

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条回答
Deceive 欺骗
2楼-- · 2020-07-17 17:08

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
查看更多
登录 后发表回答