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. Updating model from QML delegate

Updating model from QML delegate

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 105 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.
  • E Offline
    E Offline
    ECEC
    wrote last edited by
    #1

    What is the now recommended way to both access and update a C++ model's data inside a QML delegate?

    In the interest of better organising my code, I've moved my ListView's delegate into its own file, which means I can no longer use model.myRole inside the delegate.

    I've tried using required properties to pass each model role into the delegate but this appears to be read-only and does not allow me to update the model from the delegate.

    The most obvious solution to be would be to simply expose the entire model to the delegate via a property. Would this be appropriate in this case, or should I explore other options?

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

      There are two ways of doing that with required properties:

      • define a required property QtObject model and then you can write to your roles with model.role = value
      • Since Qt 6.10, views have a new property delegateModelAccess and when set to ReadWrite you can write directly to required role properties.

      More info here : https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#changing-model-data

      E 1 Reply Last reply
      1
      • GrecKoG GrecKo

        There are two ways of doing that with required properties:

        • define a required property QtObject model and then you can write to your roles with model.role = value
        • Since Qt 6.10, views have a new property delegateModelAccess and when set to ReadWrite you can write directly to required role properties.

        More info here : https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#changing-model-data

        E Offline
        E Offline
        ECEC
        wrote last edited by
        #3

        @GrecKo

        Thank you, I'm taking a look now!
        I'm curious though how this would have been handled pre-Qt 6.10, as surely wanting to update the model from the delegate is a fairly common scenario...?

        1 Reply Last reply
        0
        • GrecKoG Online
          GrecKoG Online
          GrecKo
          Qt Champions 2018
          wrote last edited by
          #4

          The first solution works pre 6.10.

          It puzzles me why the readwrite mode wasn't the default mode from the start though.

          1 Reply Last reply
          0
          • E ECEC has marked this topic as solved

          • Login

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