简体   繁体   中英

PySide2 Signal cannot receive QObject parameter

In PySide2 I have a custom defined object:

class ResizeEvent(QObject):
    def __init__(self, qresizeevent=None):
        ...

I want to a child widget emit a signal when it is resized and the parent widget receives and handles this event. So I defined a Signal in the child widget, a Slot in the parent widget, and connect them:

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)

So that each time the child widget is resized, the parent widget will receive a signal and print out a string "onResized" followed by the custom event object. But the printed event object is None , and of course the following code goes into error. If I pass some int parameters instead of a ResizeEvent parameter into the signal, I got the correct parameters. So do a QtCore.QResizeEvent parameter.

It seems that my custom defined object cannot be successfully passed through the signal-slot connection. Why is it happening? Have I missed anything?


A simplified reproducible example is presented here:

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_())

When you inherit you must call the constructor of the class that you inherit:

class ResizeEvent(QObject):
    def __init__(self, qresizeevent=None):
        super(ResizeEvent, self).__init__() # <----
        self.size = qresizeevent.size()
        self.oldSize = qresizeevent.oldSize()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM