I'm trying to make an example to make the XML dataModel change dinamically in my .qml file (when I click the button) using C++. For that, I'm returning a Qt property (GroupDataModel). But after returning the object, the ListView doesn't change, though I see the model property is returned again.
OBS: If I load it from a XMLDataModel in the .qml, instead of loading in C++ code, it works.
This is my XmlTest.hpp:
#ifndef XmlTest_HPP_
#define XmlTest_HPP_
#include <QObject>
#include <bb/cascades/GroupDataModel>
namespace bb { namespace cascades { class Application; }}
class XmlTest : public QObject
{
Q_OBJECT
Q_PROPERTY(bb::cascades::GroupDataModel* model READ model NOTIFY onModelChanged);
public:
XmlTest(bb::cascades::Application *app);
virtual ~XmlTest() {}
Q_INVOKABLE
bb::cascades::GroupDataModel *model();
Q_INVOKABLE
void setGroupDataModel();
signals:
void onModelChanged();
private:
bb::cascades::GroupDataModel *m_model;
};
#endif /* XmlTest_HPP_ */
and XmlTest.cpp:
#include "XmlTest.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <bb/data/XmlDataAccess>
using namespace bb::cascades;
using namespace bb::data;
XmlTest::XmlTest(Application *app)
: QObject(app)
{
m_model = new GroupDataModel();
qRegisterMetaType<GroupDataModel *>("GroupDataModel *");
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_xmlTest", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
app->setScene(root);
}
GroupDataModel *XmlTest::model()
{
qDebug("Returning m_model");
return m_model;
}
void XmlTest::setGroupDataModel()
{
XmlDataAccess xml;
QVariant xmlData = xml.load(QDir::currentPath() + "/app/native/assets/models/model.xml");
m_model->clear();
m_model->insertList(xmlData.toList());
qDebug("File loaded");
emit this->onModelChanged();
}
My main.qml file (just a ListView with a Button):
import bb.cascades 1.0
Page {
Container {
id: mainContainer
layout: DockLayout {}
ListView {
id: listView
dataModel: _xmlTest.model
//dataModel: XmlDataModel {
// source: "models/model2.xml"
//}
onDataModelChanged: {
console.log("Data model changed!");
}
listItemComponents: [
ListItemComponent {
type: "user"
StandardListItem {
title: ListItemData.realname
description: ListItemData.name
}
},
ListItemComponent {
type: "option"
StandardListItem {
title: ListItemData.title
}
}
]
}
Button {
text: "Click"
onClicked: {
console.log("Trying to load file");
_xmlTest.setGroupDataModel();
}
verticalAlignment: VerticalAlignment.Bottom
horizontalAlignment: HorizontalAlignment.Center
}
}
}
and the XML I'm trying to load:
<root>
<user name="myUsername" realname="My Real Name"/>
<option title="Option 1"/>
<option title="Option 2"/>
<option title="Option 3"/>
<option title="Option 4"/>
<option title="Option 5"/>
</root>