What model to use?



  • So, we have many models we can use to communicate between the QML UI and C++ back end:

    • Access QML objects from C++
    • Instantiate C++ objects in QML
    • Do C++ singletons in QML
    • Access C++ instantiations from QML
    • Do C++ plugins in QML
    • Wire up signals and slots in either direction.

    Ok, this is quite overwhelming to have so many options! I am new to QML, but a long time C/C++ guy. I wish to break my UI apart from the back. Does anyone have any suggestions on the best way to get started? I've seen the examples of all of these, and they seem fairly straightforward, but I don't have a clue which way to go!

    One example is when you have a "settings" type screen. The backend is storing app settings in a QSettings or SQL database. Do I surface the data fields from a C++ object and read/write to the UI textedits in QML? Do I use signals/slots across C++/QML to fill the qml edits and then save the changes? Do I instantiate a QML object, and use qml properties from the c++ side to pass the data to the back? Yeh, all of this would work...but which way to go? Any suggestions? Thanks!


  • Lifetime Qt Champion

    Hi,

    Well.. It depends on your use case. e.g. for the settings, there's QML's Settings type that you can use. For the rest of the application, it essentially depends on what data you want to manipulate from QML and what you want to do with it in C++.


  • Qt Champions 2016

    That is my approach:

    1. Use C++ for threading / file data model only.

    It don't handle UI specific things. It may handle user setting, but just becoz it need to be saved on file. For example , it should not provide storage for current selected items.

    1. Register C++ singleton via context property

    2. Declare Store singleton components by QML

    Store can be a wrapper of C++ model or a data model class for UI specific variables. Since C++ friendly data type may not be friendly for QML. It could do a conversion.

    1. Don't update C++ model from inline Javascript code (those in onClicked code block). Use Action / Action creator.

    Becoz I use Store component as a wrapper of C++ data model. Directly update from inline Javascript code can be harmful. Therefore, I would separate "update" and "query" into different components. Store is in fact a read only data model. And "update" should be done by "Action".

    It is difficult to tell why I use "Action" in few words. Therefore, I have written an article to explain it in deep:

    Action-Dispatcher Design Pattern for QML — Medium


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.