Solved Pass function through the QML ListModel
-
import QtQuick 2.4 import QtQuick.Controls 1.1 Item { Column { TextArea { id: txt height: 20 } Repeater { model: ListModel { Component.onCompleted: { append({index: 0, f: function() { txt.forceActiveFocus() }}) } } Component { id: d Button { text: "btn" } } delegate: Loader { sourceComponent: d onLoaded: { item.onClicked.connect(f) } } } } }
The button, generated from a ListModel, should be able to trigger an invokable that is specified in the model. How to do that?
This example should get TextArea focused when the button is clicked. Instead, it gives: 'Error: Function.prototype.connect: target is not a function'.
(In the real example, there is a C++ object with Q_INVOKABLE instead of TextArea. And the ListModel has a couple of additional fields and functions to pass.)
-
Hi @devel,
AFAIKListElement
roles cannot have scripts as its values.
http://doc.qt.io/qt-5/qml-qtqml-models-listelement.htmlValues must be simple constants; either strings (quoted and optionally within a call to QT_TR_NOOP), boolean values (true, false), numbers, or enumeration values (such as AlignText.AlignHCenter).
-
ListModel.append()
was a workaround to overcome this limitation: at least theid
s can be passed with it.I'm going to pass the
Action
objects there instead of plain functions for now.