Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. What model to use?

What model to use?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 860 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    terryfryar
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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++.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • benlauB Offline
        benlauB Offline
        benlau
        Qt Champions 2016
        wrote on last edited by benlau
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved