I have some data stored in instances of a C++ class (Data.cpp). Now i want to be able to view and edit this data from 2 seperate representations in QML, so that if the values in View1 are changed, the data itself (C++) is changed and the value displayed by View2 as well (because it gets notified when the C++ data changes).
Here is what I got so far:
Data.h
class Data : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
public:
Data(std::string name);
QString name();
void setName(const QString &n);
signals:
void nameChanged();
private:
std::string _name;
};
Parser.h (provides a list of Data)
class Parser : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<QObject*> list READ list NOTIFY listChanged)
//QList<Data*> is not working with QML :(
public:
Parser(QObject *parent = 0);
QList<QObject*> list() //stuff below is implementd in Parser.cpp
{
_list.append(new Data("name 1"));
_list.append(new Data("name 2"));
_list.append(new Data("name 3"));
return _list;
}
signals:
void listChanged();
private:
QList<QObject*> _list;
};
QML part:
ListView
{
id: view1
anchors.fill: parent
spacing: 5
delegate: Text { text: name}
model: ListModel{Component.onCompleted: getModel()}
}
ListView
{
id: list2
anchors.fill: parent
spacing: 5
delegate: Text { text: name}
model: ListModel{Component.onCompleted: getModel()}
}
function getModel()
{
var m = parser.list;
for(var i=0; i<m.length; i++)
{
list.model.append(m[i]); //simply returning the list (m) does not work
}
}
Now if I click on an item in view1 (for example) i want the name of the corresponding Data to change, and the name displayed in view2 accordingly. If I modified the name from C++, the new name should be displayed in both views.
Is there any way to do this? I'm stuck on this for days... Thanks for your help.
EDIT:
I asked a more specific question to this topic here.
It's very much possible, but you've got a few problems:
Once that's resolved, you can update a Data object simply by interacting with the reference to the current model item in your delegate.
Here's a full working example. I've added a MouseArea in the QML that changes the model and also a timer in C++ that also changes the model, to show that changes to either side are instantly reflected in the UI.
main.cpp:
main.qml: