创建者和创建的类之间的信号槽连接
我有一个问题。
我有两个类,A 和 B。 A 创建 B 类型的对象,并且还发出如下信号:
QtCore.QObject.emit(QtCore.SIGNAL('mySignal'), "Hello World")
B 可以看到 A 中的方法,因为 A 在创建 B 时将“self”作为参数传递给构造函数,如所述 这里。
现在,我想在 B 中为 A 中发出的信号编写一个槽,如下所示:
self.connect(self._creator, QtCore.SIGNAL('mySignal'), mySlot)
这里我想提一下,A 和 B 都继承自 QtCore.QObject。 mySlot 方法应该打印它从信号接收的作为参数的值。
当我运行它时,我收到此错误:
QObject.emit(SIGNAL(), ...):未绑定方法的第一个参数必须具有类型“QObject”
在两个类的 init() 中,我添加了以下内容:
QtCore.QObject.__init__(self)
不添加此内容,我收到错误:
运行时错误:底层 C/C++ 对象已被删除
我在 Qt 中没有经验。我不明白出了什么问题。请帮忙。
I have a question.
I have two classes, A and B. A creates object of type B, and also emits signals like this:
QtCore.QObject.emit(QtCore.SIGNAL('mySignal'), "Hello World")
B can see methods in A as A passed 'self' as an argument to the constructor while creating B, as described here.
Now, I want to write a slot in B for that signal emitted in A like this:
self.connect(self._creator, QtCore.SIGNAL('mySignal'), mySlot)
Here I would like to mention that both A and B inherit from QtCore.QObject. The method mySlot is just supposed to print the value it receives as the argument from the signal.
When I run it, I get this error:
QObject.emit(SIGNAL(), ...): first argument of unbound method must have type 'QObject'
In the init() of both the classes, I have added this:
QtCore.QObject.__init__(self)
Without adding this, I get the error:
RuntimeError: underlying C/C++ object has been deleted
I am not experienced in Qt. I do not understand what is going wrong. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如错误所述,您需要将
QtCore.QObject.emit
的引用传递给A
或使用实例方法调用它:或者
完全有效的示例(如果我理解正确的话):
更好的解决方案是使用 新型信号和槽
以下是您的案例示例:
As error says, you need to pass reference to
A
forQtCore.QObject.emit
or call it with instance method:Or
Fully working example (if I understood you right):
Even better solution, is to use New-style Signals and Slots
Here is example for you case: