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 `QAbstractItemModel::setData` from QML view?
Qt 6.11 is out! See what's new in the release blog

How to call `QAbstractItemModel::setData` from QML view?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 150 Views 1 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.
  • K Offline
    K Offline
    kaixoo
    wrote last edited by
    #1

    I have a question regarding the ability to call QAbstractItemModel::setData from QML code. The function setData has Q_INVOKABLE, so it should be callable from QML. However, it expects a QModelIndex& as the first argument, which doesn't seem obtainable from a QML model.

    My question, however, is more general than this. Where can I find resources about editable Qt models? I have read this article on Editable Tree Model Example but it doesn't provide insight on the QML side.

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

      If you are using required properties in your delegates, you can specify delegateModelAccess of your view to DelegateModel.ReadWrite then writing to the required properties will automatically call setData on their corresponding role.
      By default you can already do that via the model group object.

      If you really want to call QAIM::setData manually, you indeed have to pass a QModelIndex and a role but the index is accessible because the QAIM::index function is also Q_INVOKABLE.

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kaixoo
        wrote last edited by
        #3

        Sorry, this isn't clear enough to me. Do you know if there is any example code about this?

        JKSHJ 1 Reply Last reply
        0
        • K kaixoo

          Sorry, this isn't clear enough to me. Do you know if there is any example code about this?

          JKSHJ Online
          JKSHJ Online
          JKSH
          Moderators
          wrote last edited by
          #4

          @kaixoo said in How to call `QAbstractItemModel::setData` from QML view?:

          Sorry, this isn't clear enough to me. Do you know if there is any example code about this?

          See the snippet under https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#changing-model-data

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2
          • K kaixoo has marked this topic as solved
          • K Offline
            K Offline
            kaixoo
            wrote last edited by
            #5

            For multiple reasons, calling setData in this case is the only viable option. This is what I have in my head:

            ListView {
            model: /* ... */
            delegate: SomeWidget {
                 onEdit: { model.setData(model.index(row, column), someDataToMutate) }
            }
            }
            

            Column is always 0 in this case, but how is one supposed to get the current row in a QML model-view?

            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