Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-132420

Better QML API for updating QAbstractItemModel data

    XMLWordPrintable

Details

    Description

      Code
      Adapted from https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#changing-model-data

      import QtQuick
      import QtQuick.Controls.Basic
      import MyApp
      
      Window {
          width: 800
          height: 600
          visible: true
      
          ListView {
              anchors.fill: parent
              model: EditableModel {}
              delegate: TextField {
                  required property var model
                  required property string display
      
                  width: ListView.view.width
                  text: display
                  Keys.onReturnPressed: model.edit = text
              }
          }
      }
      

       

      Shortcomings
      The code above seems to be the "best" that can be done as of Qt 6.8.1. There are some unavoidable gaps:

       

      1. Can't use strong typing

      We must use the weakly-typed "required property var model". Changing it to "required property EditableModel model" causes "model.edit = text" to fail at runtime:

      Main.qml:19: TypeError: Value is null and could not be converted to an object
      

       

      2. QML tools can't understand the code

      Warning: Main.qml:19:39: Could not compile binding for onReturnPressed: Type (component in Main.qml)::model with type QVariant does not have a property edit for writing [compiler]
                  Keys.onReturnPressed: model.edit = text
                                                     ^^^^
      

      (Even if we can use required property EditableModel model, this problem persists)

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qtqmlteam Qt Qml Team User
            skoh-qt Sze Howe Koh
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes