I am a beginner in Qt and I am creating an Application by:
- Using QML for View design,
- Using python mainly for the Controller and Model part.
Therefore, QML need to interact with the python objects.
My problem: I have created a QAbstractListModel
in python via the following (simplified) code:
class MyList(QAbstractListModel):
_myCol1 = Qt.UserRole + 1
_myCol2 = Qt.UserRole + 2
def __init__(self, parent=None):
super().__init__(parent)
self.myData= [
{
'id': '01',
'name': 'test1',
},
{
'id': '02',
'name': 'test2',
}
]
def data(self, index, role=Qt.DisplayRole):
row = index.row()
if role == MyList._myCol1:
return self.myData[row]['id']
if role == MyList._myCol2:
return self.myData[row]['name']
def rowCount(self, parent=QModelIndex()):
return len(self.myData)
def roleNames(self):
return {
MyList._myCol1: b'id',
MyList._myCol2: b'name'
}
def get(self, index):
# How to implement this?
Above code works fine and exposing the list from python to QML via the QQmlApplicationEngine
and the rootContext().setContextProperty(...)
works (I used the answer from how to insert/edit QAbstractListModel in python and qml updates automatically? and the Qt for Python docs as orientation).
If using a QML ListModel
I could use the object get(index)
function as described in the docs https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html. However:
- How can I access a specific element in the instantiated MyList from QML as I would do this element with the
get(index)
method if it would be a native QML ListModel? - How to implement
get(index)
method?
I am still searching and expecting a solution referring to python and QML. Thank's for your help!