第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

QQmlPropertyMap 插入;神秘地插入無(wú),或刪除其他值

QQmlPropertyMap 插入;神秘地插入無(wú),或刪除其他值

www說(shuō) 2023-02-07 17:15:39
我在 PySide2 和 QML 工作,而 QQmlPropertyList 正在做兩件我覺(jué)得很奇怪的事情。第一個(gè)是如果 QObject 在同一語(yǔ)句中創(chuàng)建,則將其分配為值不起作用:from PySide2.QtCore import QObjectfrom PySide2.QtQml import QQmlPropertyMapqmap = QQmlPropertyMap()qmap.insert('test_key', QObject())print(qmap.value('test_key'))輸出:None也許這與 QObject 的初始化時(shí)間有關(guān)?它可以通過(guò)首先將它分配給一個(gè)變量來(lái)修復(fù):qmap = QQmlPropertyMap()obj = QObject()qmap.insert('test_key', obj)print(qmap.value('test_key'))<PySide2.QtCore.QObject(0x7fe8f26b5eb0) at 0x107075ec0>但它變得陌生。忍受我。分配兩個(gè) QObjects 工作:qmap = QQmlPropertyMap()obj = QObject()qmap.insert('test_key', obj)obj2 = QObject()qmap.insert('test_key2', obj2)print(qmap.value('test_key'))print(qmap.value('test_key2'))<PySide2.QtCore.QObject(0x7f85f7f269e0) at 0x10d32b080><PySide2.QtCore.QObject(0x7f85f7f26b70) at 0x10d32b100>我什至可以將同一個(gè)對(duì)象分配給多個(gè)鍵:qmap = QQmlPropertyMap()obj = QObject()qmap.insert('test_key', obj)qmap.insert('test_key2', obj)print(qmap.value('test_key'))print(qmap.value('test_key2'))<PySide2.QtCore.QObject(0x7fde37d7d9d0) at 0x105cdc140><PySide2.QtCore.QObject(0x7fde37d7d9d0) at 0x105cdc140>但是如果我為兩個(gè)不同的對(duì)象重用相同的變量名,第一個(gè)鍵的值會(huì)以某種方式被刪除:qmap = QQmlPropertyMap()obj = QObject()qmap.insert('test_key', obj)obj = QObject()qmap.insert('test_key2', obj)print(qmap.value('test_key'))print(qmap.value('test_key2'))None<PySide2.QtCore.QObject(0x7fcecaf072f0) at 0x111ba60c0>怎么會(huì)這樣?PySide 是否對(duì)運(yùn)行時(shí)變量名做了某種隱藏的魔法?在這個(gè)例子中它是微不足道的,但它在我的代碼中導(dǎo)致了一個(gè)很難隔離的錯(cuò)誤,我想更好地理解這一點(diǎn)。
查看完整描述

1 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊

當(dāng)您創(chuàng)建一個(gè)對(duì)象并為其分配相同的變量時(shí),前一個(gè)對(duì)象被銷(xiāo)毀,可以通過(guò)以下測(cè)試觀(guān)察到:


class Foo:

    def __del__(self):

        print("destroyed")


print("before creating Foo object: 1")

foo = Foo()

print("after creating Foo object: 1")

print("before creating Foo object: 2")

foo = Foo()

print("after creating Foo object: 2")

輸出


before creating Foo object: 1

after creating Foo object: 1

before creating Foo object: 2

destroyed

after creating Foo object: 2

destroyed

正如您所看到的,當(dāng)?shù)诙€(gè)對(duì)象被分配給同一個(gè)變量時(shí),第一個(gè)銷(xiāo)毀被調(diào)用。所以這不是 PySide2 的魔力而是 python 的魔力,同樣可以用被破壞的信號(hào)來(lái)驗(yàn)證。


from PySide2.QtCore import QObject



print("before creating QObject object: 1")

qobject = QObject()

qobject.destroyed.connect(lambda _id=id(qobject): print("destroyed1: ", _id))

print("after creating QObject object: 1")

print("before creating QObject object: 2")

qobject = QObject()

qobject.destroyed.connect(lambda _id=id(qobject): print("destroyed2: ", _id))

print("after creating QObject object: 2")

輸出:


before creating QObject object: 1

after creating QObject object: 1

before creating QObject object: 2

destroyed1:  <PySide2.QtCore.QObject(0x562c1b6ee2f0) at 0x7f4a1faece40>

after creating QObject object: 2

如果你不希望這個(gè)問(wèn)題發(fā)生,那么使用 Qt 的超能力,你可以將對(duì)象作為“qmap”的父對(duì)象傳遞,這樣所有權(quán)將由 QObject 持有,它將不再由 python 處理:


from PySide2.QtCore import QObject

from PySide2.QtQml import QQmlPropertyMap



qmap = QQmlPropertyMap()


obj = QObject(qmap)

qmap.insert("test_key", obj)


obj = QObject(qmap)

qmap.insert("test_key2", obj)


print(qmap.value("test_key"))

print(qmap.value("test_key2"))

輸出:


<PySide2.QtCore.QObject(0x559629d1e230) at 0x7fef951f01c0>

<PySide2.QtCore.QObject(0x559629d1eae0) at 0x7fef951f0240>

更新:


為了更好地理解,我們必須知道PySide2(PyQt5也適用)的QObject是Qt的QObject的包裝器,也就是說(shuō),它是這樣的:


class PyQObject:

    def __init__(self, *args):

        self._qobject_cpp = CPPQObject(*args)

        # ...

    def __del__(self):

        delete self._qobject_cpp


查看完整回答
反對(duì) 回復(fù) 2023-02-07
  • 1 回答
  • 0 關(guān)注
  • 310 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)