Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
I would like to know if it's safe to use a lot of Q_PROPERTY in a qt quick application.
My database is in c++ and I have to use this database in QML (most of the time read-only).
Thanks for your help.
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 ?
QQmlListPropertyis made to expose a list so that can be populated from QML. That's not what you want.
To expose a list of data, use
QAbstractListModel(read http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html#qabstractitemmodel-subclass). If your data is static, you could use simpler classes like
QVariantMapfor example) or
Thanks @GrecKo , I will do that
@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:
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 ?
QList<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:
Thanks @sierdzio it works with Q_INVOKABLE