使用 PyQt 将项目动态设置为 QML ListModel [英] Set items to QML ListModel dynamicaly with PyQt
问题描述
我有一个表示计划的 QML,它从数据库中获取值,所以我需要从我的 python 代码中将值插入到 ListModel
中.QML 看起来像这样:
I have a QML that represent schedule, that get values from database, so I need insert values to ListModel
from my python code. QML looks like that:
function append(newElement) {
scheduleList.model.append(newElement)
}
ListView {
id: scheduleList
model: scheduleModel
delegate: scheduleItem
section.property: "day"
section.delegate: sectionDelegate
}
Component {
id: scheduleItem
Rectangle {
Row {
spacing: 15
Text {
text: lesson
}
Text {
text: subject
}
}
}
}
Component {
id: sectionDelegate
Rectangle {
id: root
Text {
id: label
text: section
}
}
}
我是一个函数,应该向 QML ListModel 插入值:
And I a function, that should insert values to QML ListModel:
class ScheduleView(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent=parent)
self._presenter = SchedulePresenter(self)
self._widget = QQuickWidget(parent)
self._widget.rootContext().setContextProperty('scheduleView', self)
self._widget.rootContext().setContextProperty('groupsModel', self)
self._widget.setSource(QUrl('modules/schedule/Form.qml'))
def reprSchedules(self):
values = [{"lesson": "1", "subject": "PE", "day": "Monday"},
{"lesson": "2", "subject": "PE", "day": "Monday"},
{"lesson": "3", "subject": "PE", "day": "Monday"}]
#model = self._widget.rootObject().findChild(QObject, "scheduleModel")
我不知道该怎么做.请问你能帮帮我吗?我正在使用 Python2.7、PyQt5.9、QtQuick2.5
I have no ideas how to do that. Could you help me, please? I'm using Python2.7, PyQt5.9, QtQuick2.5
推荐答案
对于这个任务你可以通过QMetaObject.invokeMethod()
调用你在.qml中实现的append函数,如下图:
For this task you can invoke the append function that you have implemented in the .qml through QMetaObject.invokeMethod()
as shown below:
main.py
counter = 0
def onTimeout(obj):
global counter
value = {"lesson": str(counter), "subject": "PE", "day": QDate.longDayName(1 + counter % 7)}
QMetaObject.invokeMethod(obj, "append", Q_ARG(QVariant, value))
counter += 1
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QQuickWidget()
w.setSource(QUrl('main.qml'))
timer = QTimer()
timer.timeout.connect(lambda: onTimeout(w.rootObject()))
timer.start(1000)
w.show()
sys.exit(app.exec_())
main.qml
import QtQuick 2.0
Rectangle {
width: 640
height: 480
function append(newElement) {
scheduleModel.append(newElement)
}
ListModel {
id: scheduleModel
}
ListView {
anchors.fill: parent
id: scheduleList
model: scheduleModel
delegate: scheduleItem
section.property: "day"
section.delegate: sectionDelegate
}
Component {
id: scheduleItem
Row {
spacing: 15
Text {
text: lesson
}
Text {
text: subject
}
}
}
Component {
id: sectionDelegate
Text {
id: label
text: section
}
}
}
完整示例可在以下链接
这篇关于使用 PyQt 将项目动态设置为 QML ListModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!