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. UI in QML, logic in C++
QtWS25 Last Chance

UI in QML, logic in C++

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 3 Posters 5.4k Views
  • 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.
  • J Offline
    J Offline
    JF002
    wrote on last edited by
    #1

    Hi,

    I would like to create an application composed of a tree of QML objects (a main window composed of UI controls,...).

    I think it should be possible to define the UI appearence in QML code and all the logic in C++ code.
    For example, in the QML code, I would like to define a StackView, set its properties (anchors, size,...), but the data of the StackView should come from C++ code. And C++ code shoulb be able to respond to signals emitted from the QML code.

    Ideally, each UI module would have a corresponding C++ class that would encapsulate all the logic of module.

    However, have some difficulties to find how to do this in the documentation (it's so big that it's not easy to find exactly what I want).

    Can you provide me with a pointer in the documentation that would explain me that? Or an example?

    Thanks!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

      I think these two articles are a good start.

      http://qt-project.org/doc/qt-5.0/qtqml/qtqml-cppintegration-exposecppattributes.html

      http://qt-project.org/doc/qt-5.0/qtquick/qtquick-modelviewsdata-modelview.html#qml-data-models

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JF002
        wrote on last edited by
        #3

        Hey sorry for the delay for this answer.

        And thanks for the links, I managed to make my C++ and QML code interact with each other.

        The next step is to fill a StackView with data comming from the C++ side.
        In fact, I would like to create a menu where each entry has sub-menus and so on. The list of entries and sub-entries must be build on the C++ side. Is the StackView the better base item to do that?

        At startup, the StackView should display item coming from a model from C++.
        Then, when an item is clicked, a new list should be pushed to the stackview. This list must be filled from C++ code too.

        How could I do this? In fact I already managed to implement an "Item" class and a "ItemModel" (derived from QAbstractListModel.
        Each Item contains a list of subItems (I think that's what we call a composite pattern).
        Now, I don't know how to know which item has been clicked, and how to push its list of subitem to the stackview.

        Am I on the right path, or is there a better solution?
        Are there some examples demonstrating this kind of use case?

        Thanks!

        1 Reply Last reply
        0
        • J Offline
          J Offline
          johnco3
          wrote on last edited by
          #4

          Hi I am also doing something similar. Were you able to figure out how to control the StackView from the C++ end? For my part I would like to use the StackView with a breadcrumb bar. If you have any pointers they would be much appreciated.

          John

          1 Reply Last reply
          0
          • T Offline
            T Offline
            t3685
            wrote on last edited by
            #5

            Have a look at this (lengthy) video on how to interact between the UI and the C++ logic.

            https://www.youtube.com/watch?v=HN9mwHWH9DU

            It for one recommends that you don't push data from C++ to the QML, but rather hvae the QML request the appropriate data from the C++ logic.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              johnco3
              wrote on last edited by
              #6

              Thanks, I just finished the video, really good. Thanks.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JF002
                wrote on last edited by
                #7

                [quote author="t3685" date="1382981885"]Have a look at this (lengthy) video on how to interact between the UI and the C++ logic.

                https://www.youtube.com/watch?v=HN9mwHWH9DU

                It for one recommends that you don't push data from C++ to the QML, but rather hvae the QML request the appropriate data from the C++ logic.[/quote]

                I thought it would be better if all the logic would come from C++ side...
                Any way, I'll watch your video, it looks very interesting. Thanks!

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  t3685
                  wrote on last edited by
                  #8

                  [quote author="JF002" date="1383686140"]
                  [quote author="t3685" date="1382981885"]Have a look at this (lengthy) video on how to interact between the UI and the C++ logic.

                  https://www.youtube.com/watch?v=HN9mwHWH9DU

                  It for one recommends that you don't push data from C++ to the QML, but rather hvae the QML request the appropriate data from the C++ logic.[/quote]

                  I thought it would be better if all the logic would come from C++ side...
                  Any way, I'll watch your video, it looks very interesting. Thanks!

                  [/quote]

                  All the logic is in the C++, but it is the UI/QML that asks for data. Business/application logic in C++, UI logic in QML.
                  The data model should not know anything about how the data is displayed.

                  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