Defina itens como QML ListModel dinamicamente com PyQt
Eu tenho um QML que representa agendamento, que obtém valores do banco de dados, então eu preciso inserir valores paraListModel
do meu código python. QML é assim:
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
}
}
}
E eu uma função, que deve inserir valores no 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")
Não tenho ideias de como fazer isso. Você poderia me ajudar por favor? Estou usando Python2.7, PyQt5.9, QtQuick2.5