1 回答

TA贡献1821条经验 获得超5个赞
您的代码失败,因为返回的变量getName不是 aQObject而是 a str,类似于getAge返回 a int,因此解决方案是设置正确的签名
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
添加回答
举报