PyQt4.QtCore.pyqtSignal 对象没有属性“connect”;
我在我制作的课程中遇到了自定义信号问题。
相关代码:
self.parse_triggered = QtCore.pyqtSignal()
def parseFile(self):
self.emit(self.parse_triggered)
这两个都属于类:RefreshWidget。 在其父类中,我有:
self.refreshWidget.parse_triggered.connect(self.tabWidget.giveTabsData())
当我尝试运行该程序时,出现错误:
AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'
帮助? 提前致谢。
I'm having issues with a custom signal in a class I made.
Relevant code:
self.parse_triggered = QtCore.pyqtSignal()
def parseFile(self):
self.emit(self.parse_triggered)
Both of those belong to the class: RefreshWidget.
In its parent class I have:
self.refreshWidget.parse_triggered.connect(self.tabWidget.giveTabsData())
When I try to run the program, I get the error:
AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'
Help?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您无法在自定义类中调用
super()
或QObject.__init__()
,您也会收到该错误消息。在 Python 中的 Qt 类中定义自定义信号的清单:
__init__
调用super()
(或调用QObject.__init__()
直接。)(int)
或(str)
或((int,),(str,))
You also get that error message if you fail to call
super()
orQObject.__init__()
in your custom class.A checklist for defining custom signals in a class in Qt in Python:
__init__
callssuper()
(or callsQObject.__init__()
directly.)()
or(int)
or(str)
or((int,), (str,))
我最近开始使用 PySide(诺基亚自己的 PyQt 版本),并看到了与自定义新型信号完全相同的行为(和解决方案)。我对该解决方案最大的担忧是,当我有该类的多个实例(在我的例子中为 QThreads)时,使用类变量来保存信号会使事情变得混乱。
据我所见,
QtCore.QObject.__init__(self)
在类中找到 Signal 变量,并为实例创建该 Signal 的副本。我不知道QObject.__init__()
做什么,但生成的 Signal 执行正确的connect()
、disconnect()
和类 Signal 或在 QObject 派生类外部创建的独立 Signal 变量没有这些方法,并且不能正确使用。
I have recently started working with PySide (Nokia's own version of PyQt), and saw the exact same behaviour (and solution) with custom new-style signals. My biggest concern with the solution was that using a class variable to hold the signal would mess things up when I have multiple instances of that class (QThreads in my case).
From what I could see,
QtCore.QObject.__init__(self)
finds the Signal variable in the class and creates a copy of that Signal for the instance. I have no idea whatQObject.__init__()
does, but the resulting Signal does properconnect()
,disconnect()
andemit()
methods (and also a__getitem__()
method), whereas the class Signal or standalone Signal variables created outside of a QObject-derived class do not have these methods and can't be used properly.要使用信号/槽系统,您需要有一个 QObject 继承类。
这是一个简单的例子:
To use the signal/slot system you need to have a QObject inherited class.
Here is a simple example:
我也有同样的问题。
我忘记了,如果一个类使用信号,那么它必须继承自 QObject。我当时正在做一些重构,并没有注意到这一点。
I had the same problem.
I forgot that if a class uses Signals, then it must inherit from QObject. I was doing some re-factoring and did not pay attention to this.
为什么你直接连接信号,而你可以这样做
self.connect(widget, SIGNAL('parse_triggered()'),listener.listening_method)
?其中 self 是例如表单本身,并且可能与侦听器相同
Why do you connect directly to the signal, while you can do
self.connect(widget, SIGNAL('parse_triggered()'), listener.listening_method)
?where self is, for example, the form itself, and may be the same as listener
我和你有同样的问题。
尝试
从构造函数中移出,但在类声明中。因此,它不应该看起来像这样:
它应该看起来像这样:
这可能根本不是您正在寻找的东西,但它对我有用。无论如何,我切换回旧式信号,因为我还没有找到新式信号中具有未定义数量或类型的参数的方法。
I had the same exact problem as you.
Try moving
out of your constructor but inside your class declaration. So instead of it looking like this:
It should look like this:
This might not be at all what you are looking for, but it worked for me. I switched back to old-style signals anyways because I haven't found a way in new-style signals to have an undefined number or type of parameters.