我可以创建一个不是类的字段成员的新样式 pyqt 信号吗? [英] Can I create a new style pyqt signal that isn't a field member of a class?

查看:64
本文介绍了我可以创建一个不是类的字段成员的新样式 pyqt 信号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我可以看到使用 PyQt4 创建样式信号的唯一方法如下:

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

我的问题是,如果我在其他任何地方声明该信号,pyqt 就会向我抛出一个关于 custom_signal 没有 connect() 函数的错误.

当我想做一些简单的事情时,我想创建一个辅助函数来帮助删除样板/重复代码:启动一个新线程,在该线程中工作,将结果作为信号发送给对象.但是当我需要在类中定义信号时,这很困难.

有没有办法让信号只是一个局部变量?

解决方案

不确定我是否正确理解您的问题,但从您的评论看来,定义一个适用于任何类型的信号就足够了?如果是这种情况,您可以使用 object 作为类型:

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

简单测试:

<预><代码>>>>定义回调(结果):... 打印类型(结果)...>>>obj = MyCustomClass()>>>obj.custom_signal.connect(回调)>>>obj.custom_signal.emit('你好')<输入'str'>>>>obj.custom_signal.emit({'x': 1})<输入字典">

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?

解决方案

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'>

这篇关于我可以创建一个不是类的字段成员的新样式 pyqt 信号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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