V
V
Vitaly2017-06-15 14:14:51
Qt
Vitaly, 2017-06-15 14:14:51

Submit forms in QML, what's the best way to do it?

Good day!
An application is created on QML (still new to this) which is actually a form divided into many qml files switched through SwipeView. The form has various fields, both text and other types, it came to saving and loading data into the form.
I see, in fact, 2 ways:
1) distribute objectname to the required fields and implement saving in C ++ through findChild and then save to a file in the desired format (I plan to compressed JSON for simplicity).
2) collecting data using JS and saving by already calling the C ++ method from the forwarded class (qmlRegisterSingletonType). Data collection, as an option, each field will have Component.onCompleted where the field will "register" itself in the global object and the data will be obtained when saving by bypassing this register)
What other approaches are used in such cases? In fact, I’m still planning to implement the 1st method, but I would like to know the best practice, which somehow can’t be found in Google.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel K, 2017-06-18
@iamFake

Both of your approaches are crutches.
There is Q_PROPERTY for this , in short - inherit your class from QObject, write Q_PROPERTY, for example field1, further in QML

Item {
 id: myForm
  objectName: "myForm"
 property var formHandler;
 
 TextIntput {
    id: textInput1
   onEditingFinished: {
      formHandler.field1= textInput1.text; (textInput1 можно отбросить и написать просто text)
   }
 }
}

Find through findChild "myForm", install it through QVariant::fromValue(myFormHandler) instance of the handler class. Thus, when you finish editing the text, you automatically get the current value in the c ++ class. Further, to save values, for example, to the database, you can write some function in the slots, for example, saveForm(); and in QML, by pressing a button, call it: formHandler.saveForm();and you don’t need to collect anything from anywhere - everything will be in the prescribed Q_PROPERTY.
PS This is just one of the options =)

K
Kastuś, 2017-06-15
@Gytim

well, the third is the middle one, you can upload it to qml, produce something small in qml, and even a global one on which other forms in c ++ depend, but I didn’t really like it, it’s better either in one or in the second, it’s easier to read and search the code.
For example, I'm more into c++. I have all the infa lists. Therefore,
I store Models in c ++, buttons - forwarding in c ++ to change models. And a simple one, in qml, an example of transitions, opening windows, there is a calculator (in c ++ only the final result)
But to each his own, for me qml is a lot of possibilities and a lot of crutches, everyone can use what he wants.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question