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. How to call setData of one C++ model from another C++ model?
Forum Update on Monday, May 27th 2025

How to call setData of one C++ model from another C++ model?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 552 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.
  • M Offline
    M Offline
    milan
    wrote on last edited by
    #1

    hello, I have currently two C++ models. I would like to call setData method of C++ model from another C++ model. I also need to pass some reference doing so. I am confused how could I achieve this. Please help.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      All you need - in principle - is a valid QModelIndex (item of that model), new data and role name. And access to the other model.

      But any details depend on how your code looks, whether it's C++ or QML etc. Please provide more info.

      (Z(:^

      M 1 Reply Last reply
      1
      • sierdzioS sierdzio

        All you need - in principle - is a valid QModelIndex (item of that model), new data and role name. And access to the other model.

        But any details depend on how your code looks, whether it's C++ or QML etc. Please provide more info.

        M Offline
        M Offline
        milan
        wrote on last edited by
        #3

        @sierdzio. I have larger model which holds reference to smaller model. Larger model is used in ListView. The checked listview items are used to create smaller model. The smaller model will also communicate with hardware to update its data. So, I need to pass references or values to create smaller model which is basically subset of larger model. The smaller model is used in GridView

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          Well, that is something. So, at the moment when you need to create the smaller model, you have selected items from big models. Now, you can add a method to your C++ model (make it Q_INVOKABLE so it's available from QML) which takes item indexes (or, if your items have some unique IDs, you can use them here, too). Use that method on QML side (I imagine you have some button that user will click when items are selected - that's where this method should be used).

          Now, in the C++ method, create a new small model and send it to QML via signal. Or, if you already have the small model, just send the data to it and make sure proper signals are emitted (beginInserRows(), beginResetModel(), dataChanged() etc. depending on how you do the update).

          (Z(:^

          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