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. Dynamic qml object creation from c++ code
Forum Updated to NodeBB v4.3 + New Features

Dynamic qml object creation from c++ code

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlc++
7 Posts 4 Posters 2.2k Views 2 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.
  • F Offline
    F Offline
    FedorKozlov
    wrote on last edited by aha_1980
    #1

    I want to strongly separate app logic on c++ and gui on qt quick. I have map (2d array) of cells with deferent properties, which will be modified during app lifecycle. Size of map also can vary. I have cell.qml that describes how cell should look like. But i don't understand how to dynamically create certain amout of cells in qml from cpp code. For example, at initialization, when i create c++ object i need corresponding qml object to be created as well.

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

      Hello @FedorKozlov.

      From my point of view, if you want to trigger an action from the c++ side to qml side, you must use the signal/slot mechanism. Otherwise, you should have the object of your c++ class in qml, and if the user triggers an event, you provide a method to respond that action if it is required backend activity.

      If you provide some code it would nice, your explication it is kind of abstract.

      The truth is out there

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        For your usecase you should expose a QAbstractItemModel to your QML layer.
        Then in QML you would use a Repeater that will instantiate a Cell for each row of your model.

        F 1 Reply Last reply
        2
        • F FedorKozlov

          I want to strongly separate app logic on c++ and gui on qt quick. I have map (2d array) of cells with deferent properties, which will be modified during app lifecycle. Size of map also can vary. I have cell.qml that describes how cell should look like. But i don't understand how to dynamically create certain amout of cells in qml from cpp code. For example, at initialization, when i create c++ object i need corresponding qml object to be created as well.

          T Offline
          T Offline
          Tom_H
          wrote on last edited by
          #4

          @FedorKozlov
          It's much easier to do QML tasks in QML. Create a QML method to do it and call it from C++ with QMetaObject::invokeMethod().

          1 Reply Last reply
          0
          • GrecKoG GrecKo

            For your usecase you should expose a QAbstractItemModel to your QML layer.
            Then in QML you would use a Repeater that will instantiate a Cell for each row of your model.

            F Offline
            F Offline
            FedorKozlov
            wrote on last edited by
            #5

            Thank you all for your answers!

            I believe as @GrecKo said QAbstractItemModel is what i really need.
            Though, as i found here http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html
            QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML can only display list data. In order to display the child lists of a hierarchical model, use the DelegateModel QML type

            GrecKoG 1 Reply Last reply
            0
            • F FedorKozlov

              Thank you all for your answers!

              I believe as @GrecKo said QAbstractItemModel is what i really need.
              Though, as i found here http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html
              QAbstractItemModel presents a hierarchy of tables, but the views currently provided by QML can only display list data. In order to display the child lists of a hierarchical model, use the DelegateModel QML type

              GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              @FedorKozlov There's no need for a DelegateModel in your case.

              You can display table data with a TableView, but you can also display data in a table like fashion with a GridView or even a Repeater with the delegates positionning themselves.

              If I guessed right you are trying to do some kind of Game of Life in Qt Quick.
              The position of your cells is just data, you don't really need a table model.

              F 1 Reply Last reply
              0
              • GrecKoG GrecKo

                @FedorKozlov There's no need for a DelegateModel in your case.

                You can display table data with a TableView, but you can also display data in a table like fashion with a GridView or even a Repeater with the delegates positionning themselves.

                If I guessed right you are trying to do some kind of Game of Life in Qt Quick.
                The position of your cells is just data, you don't really need a table model.

                F Offline
                F Offline
                FedorKozlov
                wrote on last edited by FedorKozlov
                #7

                @GrecKo You're right! I am defenetly doing some kind of Game of Life)
                I've managed to create a map, using GridView and QAbstractListModel like in this video.
                I also understood how to pass values using model's roles. Now I'm trying to find out how to pass user defined classes through QVariant. I guess i also should register my 'Cell' class with qmlRegisterType<>

                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