Can I create a new style pyqt signal that isn't a field member of a class?

痞子三分冷 提交于 2019-12-24 01:52:42

问题


So for the only way that I can see to create a style signal with PyQt4 is as follows:

class MyCustomClass(QtCore.QThread):
    custom_signal = QtCore.pyqtSignal(str)

My beef is if I declare that signal anywhere else, pyqt throws an error at me about how custom_signal doesn't have a connect() function.

I would like to create a helper function to help remove the boilerplate/repeated code when I want to do something as simple as: starting a new thread, doing work in that thread, sending the result as a signal to an object. But it's hard when I need the signals to be defined in a class.

Any way to have a signal just be a local variable?


回答1:


Not sure if I'm understanding your question correctly, but from your comment it sounds like it's sufficient to define a signal that will work for any type? If that's the case, you could use object as the type:

class MyCustomClass(QtCore.QThread):
    custom_signal = QtCore.pyqtSignal(object)

Simple test:

>>> def callback(result):
...    print type(result)
...
>>> obj = MyCustomClass()
>>> obj.custom_signal.connect(callback)
>>> obj.custom_signal.emit('hello')
<type 'str'>
>>> obj.custom_signal.emit({'x': 1})
<type 'dict'>


来源:https://stackoverflow.com/questions/11199622/can-i-create-a-new-style-pyqt-signal-that-isnt-a-field-member-of-a-class

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