PySide(PyQT),怎样在非QObject对象上应用信号槽框架

        在 PySide(以及 PyQt)中,信号和槽机制是基于QObject 类实现的。只有继承自 QObject 的类才能够定义和使用信号与槽。那么如果画面中的对象不是QObject 类,就无法使用信号和槽。比如下面的代码就会出错:

import sys
from PySide6.QtCore import Signal
from PySide6.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsRectItem


class MyRectItem(QGraphicsRectItem):
    # 定义一个信号,该信号会发送一个字符串类型的参数
    signal_now_doing = Signal(str)  # 当前工况的信号

    def __init__(self, *args):
        # 调用父类 QGraphicsRectItem 的构造函数,传递所有参数
        super().__init__(*args)  # 使用父辈的 QGraphicsRectItem 的构造函数创建方框
        # 连接信号
        self.signal_now_doing.connect(self.slot)
        # 发射信号
        self.signal_now_doing.emit("temp")

    def slot(self, doing):
        # 打印接收到的参数
        print(doing)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    # 创建一个场景
    scene = QGraphicsScene()

    # 创建 MyRectItem 实例
    rect_item = MyRectItem(0, 0, 100, 100)

    # 将 MyRectItem 添加到场景中
    scene.addItem(rect_item)

    # 创建一个视图并设置场景
    view = QGraphicsView(scene)
    view.show()

    sys.exit(app.exec())

# 出错:AttributeError: 'PySide6.QtCore.Signal' object has no attribute 'connect'

        上面的代码中,QGraphicsRectItem不是QObject的子类,无法使用只有QObject才有的connect()方法,为了达到“万物皆可信号槽”的目的,可以采用下面代码方法:

import sys
from PySide6.QtCore import Signal, QObject, Slot
from PySide6.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsRectItem


class MyRectItem(QGraphicsRectItem, QObject):
    # 定义一个信号,该信号会发送一个字符串类型的参数
    signal_now_doing = Signal(str)  # 当前工况的信号

    def __init__(self, *args):
        # 调用父类 QGraphicsRectItem 的构造函数,传递所有参数
        QGraphicsRectItem.__init__(self, *args)  # 使用父辈的 QGraphicsRectItem 的构造函数创建方框
        QObject.__init__(self)  # 使用父辈的QObject的构造函数,利用它的信号槽机制
        # 连接信号
        self.signal_now_doing.connect(self.slot)
        # 发射信号
        self.signal_now_doing.emit("temp")

    @Slot(str)
    def slot(self, doing):
        # 打印接收到的参数
        print(doing)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    # 创建一个场景
    scene = QGraphicsScene()

    # 创建 MyRectItem 实例
    rect_item = MyRectItem(0, 0, 100, 100)

    # 将 MyRectItem 添加到场景中
    scene.addItem(rect_item)

    # 创建一个视图并设置场景
    view = QGraphicsView(scene)
    view.show()

    sys.exit(app.exec())

# 出错:AttributeError: 'PySide6.QtCore.Signal' object has no attribute 'connect'

        QGraphicsRectItem.__init__(self, *args) 和 QObject.__init__(self),显式地调用了父辈的构造函数并指定入参分配。

或者使用super()的方法:

import sys
from PySide6.QtCore import Signal, QObject, Slot
from PySide6.QtWidgets import QApplication, QGraphicsScene, QGraphicsView, QGraphicsRectItem


class MyRectItem(QGraphicsRectItem, QObject):
    # 定义一个信号,该信号会发送一个字符串类型的参数
    signal_now_doing = Signal(str)  # 当前工况的信号

    def __init__(self, *args):
        # 调用父类 QGraphicsRectItem 的构造函数,传递所有参数
        super(MyRectItem, self).__init__(*args)  # 使用父辈的 QGraphicsRectItem 的构造函数创建方框
        QObject.__init__(self)  # 使用父辈的QObject的构造函数,利用它的信号槽机制
        # 连接信号
        self.signal_now_doing.connect(self.slot)
        # 发射信号
        self.signal_now_doing.emit("temp")

    @Slot(str)
    def slot(self, doing):
        # 打印接收到的参数
        print(doing)


if __name__ == "__main__":
    app = QApplication(sys.argv)

    # 创建一个场景
    scene = QGraphicsScene()

    # 创建 MyRectItem 实例
    rect_item = MyRectItem(0, 0, 100, 100)

    # 将 MyRectItem 添加到场景中
    scene.addItem(rect_item)

    # 创建一个视图并设置场景
    view = QGraphicsView(scene)
    view.show()

    sys.exit(app.exec())

 修改后的代码正常运行:

PySide(PyQT),怎样在非QObject对象上应用信号槽框架_第1张图片

你可能感兴趣的:(pyside6系统学习,pyside6学习笔记,数据库)