[SailfishDevel] Technical question: populating ListModel from C++
Kimmo Lindholm
Kimmo.Lindholm at eke.fi
Fri May 23 16:11:30 UTC 2014
Thanks for help, I did it about like this for starters and it seems to work.
On my C++ function:
Q_INVOKABLE QVariantMap readEntries(QString table)
...
QSqlQuery q = QSqlQuery("SELECT * FROM params ORDER BY parameter ASC", *db);
QVariantMap tmp;
if (q.exec())
while (q.next())
tmp.insert(q.record().value("parameter").toString(),
q.record().value("value").toString());
return tmp;
On my QML:
Component.onCompleted:
{
var tmp = logger.readEntries("parameters")
for (var par in tmp)
{
parameterListModel.append({"parameter": par, "value": tmp[par]})
}
}
-kimmo
From: devel-bounces at lists.sailfishos.org [mailto:devel-bounces at lists.sailfishos.org] On Behalf Of Jarko Vihriala
Sent: Friday, May 23, 2014 11:24 AM
To: Sailfish OS Developers
Subject: Re: [SailfishDevel] Technical question: populating ListModel from C++
Actually, that's the best solution so far. Just make sure you follow the structure of the in qml/js of data inside QVariantList. Playing around QVariant and *List is a bit tricky but once you get the hang of it, it's awesome datatype.
thanks,Jarko
________________________________
From: devel-bounces at lists.sailfishos.org<mailto:devel-bounces at lists.sailfishos.org> [devel-bounces at lists.sailfishos.org] on behalf of Mikko Leppänen [mleppan23 at gmail.com]
Sent: Friday, May 23, 2014 11:20 AM
To: Sailfish OS Developers
Subject: Re: [SailfishDevel] Technical question: populating ListModel from C++
Hi,
Or if you don't want to subclass e.g. qabstractitemmodel, you can always return a QVariantList(or QList) from c++ and expose that sequence to QML. Then use that JS array to populate ListModel.
/Mikko
2014-05-23 10:53 GMT+03:00 Markus Svensson <markus.svensson at me.com<mailto:markus.svensson at me.com>>:
Hi,
I have done the same thing in my Sailfish learning project. It was way more complicated than I would have liked it to be (or maybe I did it in the wrong way...).
Feel free to have a look at how I implemented it: https://github.com/Ortofta/SilicaNote
The code quality is probably not the best - these are my first baby steps in QT and Sailfish. ;)
Regards,
Markus
23 maj 2014 kl. 08:52 skrev Kimmo Lindholm <Kimmo.Lindholm at eke.fi<mailto:Kimmo.Lindholm at eke.fi>>:
Hi,,
Thank you Janne and Jarko, I will take a look on both of these approaches;
> use the QSqlQueryModel in your c++ and expose that to QML ; just like in : http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML
> subclassing QAbstractItemModel. See http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html and http://qt-project.org/doc/qt-5/qabstractitemmodel.html to get started.
-kimmo
_______________________________________________
SailfishOS.org<http://SailfishOS.org> Devel mailing list
To unsubscribe, please send a mail to devel-unsubscribe at lists.sailfishos.org<mailto:devel-unsubscribe at lists.sailfishos.org>
_______________________________________________
SailfishOS.org Devel mailing list
To unsubscribe, please send a mail to devel-unsubscribe at lists.sailfishos.org<mailto:devel-unsubscribe at lists.sailfishos.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.sailfishos.org/pipermail/devel/attachments/20140523/1b7e039b/attachment-0001.html>
More information about the Devel
mailing list