problem with get values from QHash in qml
-
hi i want to get rows from a sqlite database and insert them into a listview
i made a function which returns QList<QHash<QString, QVariant>>
everything works fine but i can't get keys and values from the hashlook at below codes
my c++ code
QList<QHash<QString, QVariant>> DataBase::getRows() { _db.open(); if(_query.exec("select * from Persons")) { qInfo() << "Rows returned successfully"; QList<QHash<QString, QVariant>> rows; QHash<QString, QVariant> columnsValue; while (_query.next()) { columnsValue["id"]= _query.value("id"); columnsValue["Name"] = _query.value("Name"); rows.append(columnsValue); } _db.close(); return rows; } else { qInfo() << "return rows failed"; qInfo() << _query.lastError(); _db.close(); } _db.close(); }
my qml code:
ListView { anchors.fill: parent model: ListModel { Component.onCompleted: { var personsRows = DataBase.getRows() for(var i = 0; i < personsRows.length; i++) { print(personsRows[i]["id"]); } } } delegate: Label { text: index } }
but it returns undefined
thanks in advance
-
QML does not understand such types. You need to use QVariant, QVariantMap, QVariantHash etc.
Or make a proper model with roles and then you can get data in QML via named roles roleNames().
-
QML does not understand such types. You need to use QVariant, QVariantMap, QVariantHash etc.
Or make a proper model with roles and then you can get data in QML via named roles roleNames().