Save data from multiple Qt components scattered around multiple QML files
-
I would like to save the current "value" property of several components (e.g. a Slider) as a configuration profile when the user clicks on the Save button in my application. However, the Save button is in a different file than the rest of the components. How should I deal with this? Some solutions that I have thought:
- do a two-way binding between the "value" property of the component and a role in my model (by using a Binding)
- reference every component by ID, even if they are in different QML files, in the onClicked handler in my Save button and manually save the data to the model
- make onClicked in my Save button to emit a signal that will be "caught" in every QML file, making the components commit their value to my model
Am I on the right track here?
-
when the save button is clicked you should emit signal & set the value in appropriate objects is Qml components. Then component sholud take care saving using property binding. When model, is it a model as in model view framework. Or is it just a c++ object ?
-
My model is a C++ object based on QSqlTableModel.
However, I think you got it backwards. I don’t want to SET the values of my QML componentes - I want to READ their values and store them in the model. Since they are in different files, I wonder what are the good practices regarding this scenario.
-
2nd and 3rd option look same. All other QML components are created when you are in save button screen. So you should have id of object with you. When you click on the save button, value should be saved in particular index of the model. So this signal should be propagated to each and every component. They should take care of saving the data to appropriate index of the model. When you say QML components are they used as delegate objects ?
-
@dheerendra I don’t have the IDs with me. They are in another scope. My Save button is in a side panel that shares the same parent as the QML components, but trying to access them by ID fails. The only thing that seems to work is the two way binding, but that looks overly complicated. Data models in QML are hard :)
-
Any sample to demonstrate your stuff ? I'm wondering why they are not accessible.
-
@dheerendra Will try to set a minimal example, but I have a StackView where each view is a different QML file. I can't get the ID of a component in view A from view B.
-
If it is stack view you can use the get(index) method to get the object and call appropriate methods. Or is it something else ?