如何直接从Python向QML中的ListModel添加新元素

时间:2018-04-03 13:00:03

标签: python pyqt qml pyqt5

我是Python / QML的新手,我似乎无法弄清楚如何直接从Python向ListModel添加新的ListElements。这是代码:

Python文件:

import sys
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QObject
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine('tutorial.qml')
sys.exit(app.exec_())

和qml文件:

import QtQuick 2.0
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3

ApplicationWindow {
    visible: true
    title: qsTr("Window Title")
     ListModel {
        id: listmodelid
        objectName: "listmodelobject"
            ListElement { 
                name: "Oliver A" 
                student:false
                credits:2
            }
            ListElement { 
                name: "Rick Liam" 
                student:true
                credits:4
            }
    }

    ListView {
        model: listmodelid
        height: 200
        delegate: ItemDelegate{
            text: name
        }
    }
}

我想从python中的这个列表中添加新元素;

some_list=[{"name":"John Doe","credits":3,"student":True},{"name":"John Deer","credits":7,"student":True},{"name":"John Cena","credits":0,"student":False}]

我尝试使用.setProperty('model',some_list)获取ListModel对象,但似乎没有做任何事情。

0 个答案:

没有答案