1 回答

TA貢獻1821條經(jīng)驗 獲得超5個贊
您的代碼失敗,因為返回的變量getName不是 aQObject而是 a str,類似于getAge返回 a int,因此解決方案是設(shè)置正確的簽名
import sys
from PySide2.QtCore import Property, QObject, QCoreApplication
class MyItem(QObject):
def __init__(self, parent=None):
super(MyItem, self).__init__(parent)
self.name = "John"
self.age = 22
@Property(str, constant=True)
def getName(self):
return self.name
@Property(int, constant=True)
def getAge(self):
return self.age
if __name__ == "__main__":
app = QCoreApplication(sys.argv)
item = MyItem()
print(item.property("getName"))
print(item.property("getAge"))
輸出:
John
22
添加回答
舉報