Custom Slot decorator not working in PySide6 when connecting inherited event

118 Views Asked by At

I had the following piece of code working fine in PySide2 project:

def _my_slot(func):
    def wrapper(*args, **kwargs):
        func(*args, **kwargs)
        obj = args[0]
        obj.activateWindow()
        obj.raise_()
        obj.setFocus()

    return Slot()(wrapper)

I could then use @_my_slot as a decorator around slots where I wanted to give back focus to the parent after they're executed.

I'm trying to migrate this project to PySide6, and this piece of code is not working anymore with the following error

AttributeError: 'PySide6.QtCore.MetaFunction' object has no attribute 'im_func'

any has an idea what I'm doing wrong?


This is a minimal example to reproduce, python 3.11, PySide6 6.6.0, windows:

import sys

from PySide6.QtWidgets import QApplication
from PySide6.QtWidgets import QMainWindow
from PySide6.QtCore import Slot, Signal, QPoint


def _my_slot(func):
    """
    A decorator that adds focus and window activation functionality to slots.
    """

    def wrapper(*args, **kwargs):
        func(*args, **kwargs)
        obj = args[0]
        obj.activateWindow()
        obj.raise_()
        obj.setFocus()

    return Slot()(wrapper)


class MainWindow(QMainWindow):
    total_number = Signal(int)

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        # if I comment the following line it works
        self.customContextMenuRequested.connect(self.onContextMenu)
        self.total_number.connect(self.update_total_number)

    @Slot()
    def onContextMenu(self, point: QPoint):
        pass

    @_my_slot
    def update_total_number(self, total_number: int):
        self.total_number.emit(total_number)


if __name__ == "__main__":
    app = QApplication()
    window = MainWindow()
    sys.exit(app.exec())
0

There are 0 best solutions below