[solved] change external data model in qml
-
Hello,
I created model in c++ code, something like this :
@
class EditorNode : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
...
}QVariantList test;
test << QVariant::fromValue((QObject*)new EditorNode());
connect(((EditorNode*)test.at(0).value<QObject*>()), SIGNAL(nameChanged()), this, SLOT(test()));
qmlView->rootContext()->setContextProperty("nodesList", test);
@and I'm trying to change data from qml
@
Repeater
{
model: nodesList;
delegate: Rectangle
{
width:80; height:50;
TextEdit
{
anchors.fill: parent;
text:model.modelData.name; // actually this should be binding, not copying
}
}
}
@but when I change text in textview, nothing happens to model (dont recieve slot call), how can I solve this problem ?
-
well, I have tried different approaches and came to the problem with MVC pattern, and property bindings in qml
example :
@
// some really simple code
Item
{
property int a: 1; // model
property int b: a; // data in view (for example we bind text string to text property in textview)
onAChanged: {console.log("a changed to ", a);}
Component.onCompleted:
{
console.log("change model directly : ");
a = 2;
console.log("change model indirectly through binding : ");
b = 4; // I really want get model data here, but this line only do assignment to property in view part
}
}
@I have not found any mechanism for obtaining the primary source of data, and only one approach works :
@
Item
{
property int a: 1;
property int b: 1;
onAChanged: {console.log("a changed to ", a);}
onBChanged: {a = b;}
Component.onCompleted:
{
console.log("change model directly : ");
a = 2;
console.log("change model indirectly through binding : ");
b = 4;
}
}
@and this how use it with data models
@
ListModel
{
id: foo;
ListElement {a: 2;}
}Repeater
{
model: foo;
delegate: Item
{
property int b: 0;
onBChanged: {foo.setProperty(index, "a", b);}Component.onCompleted:
{
b = 1;
}
}
}
@but why model delegate (view-controller) must acts as a data-source "proxy" by default ? when in most cases when we change "proxy" property we just need to change primary source of data without knowing it's name
It shouldn't be that hard, because internal binding mechanism in qt contains all information for getting data source, is there any simple way of doing this ? just something like Qt.getBindingOrigin(b) that return id a will work -
so, solution : weak link by hand :)
@
Repeater
{
model: nodesList;
delegate: Rectangle
{
width:80; height:50;
TextEdit
{
anchors.fill: parent;
onTextChanged: {nodesList[index].name = text;}
Component.onCompleted: {text = nodesList[index].name;}
}
}
}
@ps. please close the thread, thanks