Q_PROPERTY c++/QML
-
Yes it is safe. QML uses only Qt properties to achieve its functionality. Everything you set (like
color: "red"
in a Rectangle) is a Q_PROPERTY. -
Your idea is to expose the values present in Database. You would like to read them & pass the values to QML. Hope this is right. As @sierdzio suggested already, you can use Q_PROPERTY. It is designed for this. You can also expose the list to QML side as Q_PROPERTY instead of individual Q_PROPERTY.
-
Ok thanks for your replies.
@dheerendra you suggested to expose the list to QML and it's a good idea, QQmlListProperty will do the job ? Or I should use an other type ? -
QQmlListProperty
is made to expose a list so that can be populated from QML. That's not what you want.
To expose a list of data, useQAbstractListModel
(read http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass). If your data is static, you could use simpler classes likeQStringList
,QVariantList
(composed ofQVariantMap
for example) orQObjectList
. -
@GrecKo as you said I expose a QVariantList like this:
//c++ Q_PROPERTY(QList<QVariant> gav_GlobalDb READ gav_get_GlobalDb WRITE gv_set_GlobalDb) QList<QVariant> gav_get_GlobalDb(void); void gv_set_GlobalDb(QList<QVariant> oav_GlobalDb);
I can read a value from QML like this:
//QML g_GlobalDatabase.gav_GlobalDb[VAR_INDEX]
But I don't find how to write a value... I tried this but it doesn't work:
//QML g_GlobalDatabase.gav_GlobalDb[VAR_INDEX] = true;
Can you help me this that ?
Thanks
-
You're assigning
bool
toQList<QVariant>
, no wonder it does not work. I do not know how to nicely fix this.A hacky solution would be to add a Q_INVOKABLE method with the following signature:
Q_INVOKABLE void setGlobalDb(int index, const QVariant &value);
Then from QML you could do:
g_GlobalDatabase.setGlobalDb(VAR_INDEX, true)