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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PySide2 在訪問 QObject::property() 時崩潰

PySide2 在訪問 QObject::property() 時崩潰

阿晨1998 2022-05-11 15:52:50
所以我不確定這是一個錯誤還是什么,但我花了很長時間來解決這個問題,但無法解決。訪問調(diào)用QObject::property()函數(shù)時會出現(xiàn)問題。這是一個最小的可重現(xiàn)示例:import sysfrom PySide2 import QtCorefrom PySide2.QtWidgets import QApplicationfrom PySide2.QtCore import Qt, QCoreApplication, QObject, Slotfrom PySide2.QtQml import QQmlApplicationEngine, QQmlContextclass MyItem(QObject):    def __init__(self):        super(MyItem, self).__init__()        self.name = "John"        self.age = 22    @QtCore.Property(QtCore.QObject, constant=True)    def getName(self):        return self.name    @QtCore.Property(QtCore.QObject, constant=True)    def getAge(self):        return self.ageif __name__ == '__main__':    app = QApplication(sys.argv)    provider = MyModelProvider()    item = MyItem()    print(item.property("getName")) # the program crashes here    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)    QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)    engine = QQmlApplicationEngine()    engine.rootContext().setContextProperty('provider', provider)    engine.load('qml/main.qml')    sys.exit(app.exec_())該程序總是崩潰并顯示以下輸出:Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
查看完整描述

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


查看完整回答
反對 回復 2022-05-11
  • 1 回答
  • 0 關(guān)注
  • 257 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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