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

Better QML API for updating QAbstractItemModel data

XMLWordPrintable

    • 4bd5b3127 (dev), f469d538a (dev), 3bc0a1140 (dev), 1cf72c90d (dev), 9ee136918 (dev), 495de8d46 (dev), 58f50363b (dev), ea39961f7 (dev), 8bc307e8e (dev), 335299d57 (6.10), 7873f3484 (dev), c5832c09c (dev), 8f02d6f0c (6.10), 4d9841d39 (6.10)

      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)

        For Gerrit Dashboard: QTBUG-132420
        # Subject Branch Project Status CR V

            ulherman Ulf Hermann
            skoh-qt Sze Howe Koh
            Votes:
            1 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: