PySide2 Signal 无法接收 QObject 参数
PySide2 Signal cannot receive QObject parameter
在 PySide2
我有一个自定义对象:
class ResizeEvent(QObject):
def __init__(self, qresizeevent=None):
...
我希望子小部件在调整大小时发出信号并且父小部件接收并处理此事件。所以我在子部件中定义了一个Signal
,在父部件中定义了一个Slot
,并将它们连接起来:
class ChildWidget(QWidget):
resized = Signal(ResizeEvent)
class ParentWidget(QWidget):
@Slot(ResizeEvent)
def onResized(self, event):
print("onResized", event)
...
...
def __init__(self):
...
self.child.resized.connect(self.onResized)
这样,每次调整子控件的大小时,父控件都会收到一个信号并打印出一个字符串 "onResized",后跟自定义事件对象。但是打印出来的事件对象是None
,下面的代码当然会出错。如果我将一些 int
参数而不是 ResizeEvent
参数传递给信号,我得到了正确的参数。 QtCore.QResizeEvent
参数也是如此。
我的自定义对象似乎无法通过信号槽连接成功传递。为什么会这样?我错过了什么吗?
此处提供了一个简化的可重现示例:
import sys
from PySide2.QtCore import QObject, Signal, Slot
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
class ResizeEvent(QObject):
def __init__(self, qresizeevent=None):
self.size = qresizeevent.size()
self.oldSize = qresizeevent.oldSize()
class ChildWidget(QWidget):
resized = Signal(ResizeEvent)
def resizeEvent(self, event):
super().resizeEvent(event)
self.resized.emit(ResizeEvent(event))
class ParentWidget(QWidget):
def __init__(self):
super().__init__()
self.child = ChildWidget()
self.child.setFixedSize(200,50)
self.button = QPushButton("Increase Size")
self.layout = QVBoxLayout()
self.layout.addWidget(self.child)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.child.resized.connect(self.onResized)
self.button.clicked.connect(self.onClicked)
@Slot(bool)
def onClicked(self, checked):
self.child.setFixedSize(self.child.width() + 10, self.child.height() + 10)
@Slot(ResizeEvent)
def onResized(self, event):
print("onResized", event)
if __name__ == "__main__":
app = QApplication()
widget = ParentWidget()
widget.show()
sys.exit(app.exec_())
当您继承时,您必须调用您继承的 class 的构造函数:
class ResizeEvent(QObject):
def __init__(self, qresizeevent=None):
super(ResizeEvent, self).__init__() # <----
self.size = qresizeevent.size()
self.oldSize = qresizeevent.oldSize()
在 PySide2
我有一个自定义对象:
class ResizeEvent(QObject):
def __init__(self, qresizeevent=None):
...
我希望子小部件在调整大小时发出信号并且父小部件接收并处理此事件。所以我在子部件中定义了一个Signal
,在父部件中定义了一个Slot
,并将它们连接起来:
class ChildWidget(QWidget):
resized = Signal(ResizeEvent)
class ParentWidget(QWidget):
@Slot(ResizeEvent)
def onResized(self, event):
print("onResized", event)
...
...
def __init__(self):
...
self.child.resized.connect(self.onResized)
这样,每次调整子控件的大小时,父控件都会收到一个信号并打印出一个字符串 "onResized",后跟自定义事件对象。但是打印出来的事件对象是None
,下面的代码当然会出错。如果我将一些 int
参数而不是 ResizeEvent
参数传递给信号,我得到了正确的参数。 QtCore.QResizeEvent
参数也是如此。
我的自定义对象似乎无法通过信号槽连接成功传递。为什么会这样?我错过了什么吗?
此处提供了一个简化的可重现示例:
import sys
from PySide2.QtCore import QObject, Signal, Slot
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
class ResizeEvent(QObject):
def __init__(self, qresizeevent=None):
self.size = qresizeevent.size()
self.oldSize = qresizeevent.oldSize()
class ChildWidget(QWidget):
resized = Signal(ResizeEvent)
def resizeEvent(self, event):
super().resizeEvent(event)
self.resized.emit(ResizeEvent(event))
class ParentWidget(QWidget):
def __init__(self):
super().__init__()
self.child = ChildWidget()
self.child.setFixedSize(200,50)
self.button = QPushButton("Increase Size")
self.layout = QVBoxLayout()
self.layout.addWidget(self.child)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.child.resized.connect(self.onResized)
self.button.clicked.connect(self.onClicked)
@Slot(bool)
def onClicked(self, checked):
self.child.setFixedSize(self.child.width() + 10, self.child.height() + 10)
@Slot(ResizeEvent)
def onResized(self, event):
print("onResized", event)
if __name__ == "__main__":
app = QApplication()
widget = ParentWidget()
widget.show()
sys.exit(app.exec_())
当您继承时,您必须调用您继承的 class 的构造函数:
class ResizeEvent(QObject):
def __init__(self, qresizeevent=None):
super(ResizeEvent, self).__init__() # <----
self.size = qresizeevent.size()
self.oldSize = qresizeevent.oldSize()