Accessing property by name or by READ function ?
-
Hello,
I have a list of model in a property declared as follow :
Q_PROPERTY(QQmlListProperty<MyModel> listModel READ getListModel NOTIFY listModelChanged)
I use this property in QML in a ListView as follow (only relevant code is shown):
ListView { model: Backend.getListModel() }
My question is, should I declare my ListView model with the READ function (
model: Backend.getListModel()
) or just with the property name (model: Backend.listModel
) ?
Is there any difference between these two options ?PS : I am working with Qt 5.14.2 with MinGW (windows)
-
@Mixlu said in Accessing property by name or by READ function ?:
My question is, should I declare my ListView model with the READ function (model: Backend.getListModel()) or just with the property name (model: Backend.listModel()) ?
Is there any difference between these two options ?Yes, there is a big difference!
By usingmodel: Backend.getListModel()
the affectation will be static,model
will be set once at Item load.
Withmodel: Backend.listModel
the affectation will be dynamic, on eachlistModelChanged
signal, model will re-readBackend.getListModel()
. -
Note that in general you should not mark your properties' READ functions as slot or Q_INVOKABLE, just make it a simple public function.
This way you can only access it by name and have a clear QML api. -
@fcarney This will be configurable in Qt Creator 4.15 : https://bugreports.qt.io/browse/QTCREATORBUG-15779 https://codereview.qt-project.org/c/qt-creator/qt-creator/+/310832