PyQt-GraphicsScene-GraphicsItem传参

前言

QGraphicsItem如何调用自定义scene中变量

一、scene()方法

每个QGraphicsItem都有一个scene()方法,它返回该项目所属的场景,可以直接通过这个方法来访问它。

class MyScene(QGraphicsScene):
    def __init__(self):
        super(MyScene,self).__init__()
       	self.custom_variable=1
       
class MyItem(QGraphicsItem):
    def __init__(self parent=None):
        super(MyItem,self).__init__(parent)
    
    def someMethod(self):
        custom_scene = self.scene()
        if custom_scene:
            print(custom_scene.custom_variable)

二、通过构造函数传递

当创建一个新的QGraphicsItem时,可以通过其构造函数将所需的数据或引用传递给它。

class MyScene(QGraphicsScene):
    def __init__(self):
        super(MyScene,self).__init__()
       	self.custom_variable=1

class MyItem(QGraphicsItem):
    def __init__(self, custom_value, parent=None):
        super().__init__(parent)
        self.custom_variable = custom_variable

my_scene = MyScene()
my_item = MyItem(my_scene.custom_variable)

问题:当my_scene的值发生改变时,my_item中却没有改变

解决:

1.传递对象而非值

Python中的变量都是对象引用。当你传递一个对象,如列表、字典、或自定义对象,你实际上是传递这个对象的引用,而不是它的拷贝。这意味着如果你在函数或类中修改了传递进来的对象,这个修改会影响到所有引用这个对象的地方。

这和基本数据类型如整数、字符串或元组等是不同的。这些类型是不可变的,所以传递的是其值,而不是引用。

class MyScene(QGraphicsScene):
    def __init__(self):
        super(MyScene,self).__init__()
       	self.custom_variable=1

class MyItem(QGraphicsItem):
    def __init__(self, scene, parent=None):
        super().__init__(parent)
        self.scene = scene
		print(self.scene.custom_variable)
my_scene = MyScene()
my_item = MyItem(my_scene)

三、使用python属性

Python是动态语言,允许您在实例化之后为对象动态添加属性。这意味着您可以在场景创建项目后,直接为QGraphicsItem实例设置新的属性。

class MyScene(QGraphicsScene):
    def __init__(self):
        super(MyScene,self).__init__()
       	self.custom_variable=1

class MyItem(QGraphicsItem):
    def __init__(self, parent=None):
        super().__init__(parent)

    def someMethod(self):
        print(self.custom_variable)	

my_scene = MyScene()
my_item = MyItem()
my_item.custom_variable = my_scene.custom_variable # 添加动态属性
my_item.someMethod()  

四、信号和槽

信号和槽是Qt的一个核心特性,允许对象之间的通信。当某个事件发生时,可以发出一个信号;这个信号可以连接到一个槽,当信号发出时,槽中的代码会被执行。
Python @property属性详解
PyQt 信号与槽(Signal&Slot)

from PyQt5.QtCore import pyqtSignal

class MyScene(QGraphicsScene):
    itemAdded = pyqtSignal(object) 
    variableChanged = pyqtSignal(object)

    def __init__(self):
        super(MyScene,self).__init__()
        self.variable = 1

    def addNewItem(self, item):
        self.addItem(item)
        self.itemAdded.emit(self.variable)

    @property
    def custom_variable(self):
        return self.variable

    @custom_variable.setter
    def custom_variable(self, value):
        self.variable = value
        self.variableChanged.emit(value)

class MyItem(QGraphicsItem):
    def __init__(self, parent=None):
        super(MyItem,self).__init__(parent)

    def custom_value(self, value):
        self.custom_variable = value

    def update_custom_value(self, new_value):
        self.custom_variable = new_value

my_scene = MyScene()
my_item = MyItem()
my_scene.itemAdded.connect(my_item.custom_value)
my_scene.addNewItem(my_item) # 相对于初始化参数
# 关联scene中的变量
my_scene.variableChanged.connect(my_item.update_custom_value)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值