pyqt5 中的连接功能不起作用 [英] connect function in pyqt5 does not work
问题描述
我最近从 pyside
迁移到 pyqt5
并且有问题.我在网上查了一下,显然,它已经发生在使用 pyqt4
并转移到 pyqt5
的人身上.但是,它并没有真正帮助...我尝试在 Qobject
之后添加 pyqtSignal
但它仍然无法正常工作.请帮忙.这些是我的代码行:
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),Dialog.accept)QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
这是我运行时出现的:
AttributeError: type object 'QObject' 没有属性 'connect'
来自文档:
connect(slot[, type=PyQt5.QtCore.Qt.AutoConnection[, no_receiver_check=False]])
<块引用>
将信号连接到插槽.如果连接失败,将引发异常.参数:
slot
– 要连接的插槽,可以是 Python 可调用信号或其他绑定信号.type
– 要建立的连接的类型.no_receiver_check
– 抑制底层 C++ 接收器实例仍然存在的检查,并无论如何传递信号.
<小时>
例如:
self.buttonBox.accepted.connect(Dialog.accept) # pyqt5
<块引用>
QtCore.QObject.connect(self.buttonBox.rejected, Dialog.reject) # pyqt4
作为旁注,Dialog"听起来像一个类,您可能想要连接到一个实例,否则考虑使用小写前写字母命名您的实例...
I recently moved from pyside
to pyqt5
and there is a problem. I looked it up online and apparently, it already happened to people who used pyqt4
and moved to pyqt5
. However, it didn't really help... I tried to add pyqtSignal
after Qobject
but it is still not working. Please help.
these are my code lines:
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"),Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
and this is what appears when I run it:
AttributeError: type object 'QObject' has no attribute 'connect'
connect(slot[, type=PyQt5.QtCore.Qt.AutoConnection[, no_receiver_check=False]])
Connect a signal to a slot. An exception will be raised if the connection failed. Parameters:
slot
– the slot to connect to, either a Python callable or another bound signal.type
– the type of the connection to make.no_receiver_check
– suppress the check that the underlying C++ receiver instance still exists and deliver the signal anyway.
for your example:
self.buttonBox.accepted.connect(Dialog.accept) # pyqt5
QtCore.QObject.connect(self.buttonBox.rejected, Dialog.reject) # pyqt4
As a sidenote, "Dialog" sounds like a class, you probably want to connect to an instance, else think about naming your instances with lowercase front-letters ...
这篇关于pyqt5 中的连接功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!