Details
-
Suggestion
-
Resolution: Unresolved
-
P2: Important
-
None
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)