创建者和创建的类之间的信号槽连接

发布于 2025-01-02 19:32:00 字数 805 浏览 8 评论 0原文

我有一个问题。

我有两个类,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2025-01-09 19:32:00

未绑定方法的第一个参数必须具有类型“QObject”

如错误所述,您需要将 QtCore.QObject.emit 的引用传递给 A 或使用实例方法调用它:

QtCore.QObject.emit(self, QtCore.SIGNAL('mySignal'), "Hello World")

或者

self.emit(QtCore.SIGNAL('mySignal'), "Hello World")

完全有效的示例(如果我理解正确的话):

from PyQt4 import QtCore

class A(QtCore.QObject):
    mySignal = QtCore.SIGNAL('mySignal(QString)')
    def __init__(self):
        QtCore.QObject.__init__(self)

    def create_b(self):
        return B(self)

    def some_action(self):
        QtCore.QObject.emit(self, QtCore.SIGNAL('mySignal'), "Hello World")
        # this will work too
        self.emit(QtCore.SIGNAL('mySignal'), "Hello World")

class B(QtCore.QObject):
    def __init__(self, creator):
        QtCore.QObject.__init__(self)
        self._creator = creator
        self.connect(self._creator, QtCore.SIGNAL('mySignal'), self.mySlot)

    def mySlot(self, str):
        print str

a = A()
b = a.create_b()
a.some_action()

更好的解决方案是使用 新型信号和槽

以下是您的案例示例:

from PyQt4 import QtCore

class A(QtCore.QObject):
    mySignal = QtCore.pyqtSignal(str)
    def __init__(self):
        QtCore.QObject.__init__(self)

    def create_b(self):
        return B(self)

    def some_action(self):
        self.mySignal.emit("Hello World")

class B(QtCore.QObject):
    def __init__(self, creator):
        QtCore.QObject.__init__(self)
        self._creator = creator
        self._creator.mySignal.connect(self.mySlot)

    def mySlot(self, str):
        print str

a = A()
b = a.create_b()
a.some_action()

first argument of unbound method must have type 'QObject'

As error says, you need to pass reference to A for QtCore.QObject.emit or call it with instance method:

QtCore.QObject.emit(self, QtCore.SIGNAL('mySignal'), "Hello World")

Or

self.emit(QtCore.SIGNAL('mySignal'), "Hello World")

Fully working example (if I understood you right):

from PyQt4 import QtCore

class A(QtCore.QObject):
    mySignal = QtCore.SIGNAL('mySignal(QString)')
    def __init__(self):
        QtCore.QObject.__init__(self)

    def create_b(self):
        return B(self)

    def some_action(self):
        QtCore.QObject.emit(self, QtCore.SIGNAL('mySignal'), "Hello World")
        # this will work too
        self.emit(QtCore.SIGNAL('mySignal'), "Hello World")

class B(QtCore.QObject):
    def __init__(self, creator):
        QtCore.QObject.__init__(self)
        self._creator = creator
        self.connect(self._creator, QtCore.SIGNAL('mySignal'), self.mySlot)

    def mySlot(self, str):
        print str

a = A()
b = a.create_b()
a.some_action()

Even better solution, is to use New-style Signals and Slots

Here is example for you case:

from PyQt4 import QtCore

class A(QtCore.QObject):
    mySignal = QtCore.pyqtSignal(str)
    def __init__(self):
        QtCore.QObject.__init__(self)

    def create_b(self):
        return B(self)

    def some_action(self):
        self.mySignal.emit("Hello World")

class B(QtCore.QObject):
    def __init__(self, creator):
        QtCore.QObject.__init__(self)
        self._creator = creator
        self._creator.mySignal.connect(self.mySlot)

    def mySlot(self, str):
        print str

a = A()
b = a.create_b()
a.some_action()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文