02-16-2013 04:08 PM
Hello again Dear Developers!
I am wondering, it is possible to display every change in content of my list immediately after it introduction?
I mean - I have list (XMLListModel) and I fill each record of list by the textfield. Everything works great but my entries are visible after relaunch application. So it will be great if every change would be display immediately. Can you give me any solutions?
Greetings
My code(main.qml):
// Default empty project template
import bb.cascades 1.0
// creates one page with a label
Page {
Container {
layout: StackLayout {}
Label {
id: label
text: "Test app"
textStyle.base: SystemDefaults.TextStyles.BigText
}
TextField {
id: textfield
hintText: "Enter Text..."
}
Button {
text: "Add"
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
onClicked: {
mainObj.add(textfield.text)
}
}
Button {
text: "Save"
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
onClicked: {
mainObj.save()
}
}
ListView {
dataModel: XmlDataModel {
source: mainObj.sciezka()
}
listItemComponents: [
ListItemComponent {
type: "listItem"
Container {
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
CheckBox {
checked: ListItemData.checked
}
Label {
text: ListItemData.task
textStyle {
base: SystemDefaults.TextStyles.TitleText
fontWeight: FontWeight.Normal
}
}
}
}
]
}
}
}
and the content of cpp file:
// Default empty project template
#include "Test.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QFile>
#include <QTextStream>
#include <QtXml>
using namespace bb::cascades;
QFile file(QDir::homePath()+"/items.xml");
QDomDocument baza;
QDomElement korzen = baza.createElement("root");
Test::Test(bb::cascades::Application *app)
: QObject(app)
{
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(th is);
baza.appendChild(korzen);
qml->setContextProperty("mainObj",this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
app->setScene(root);
}
void Test::add(QString data){
QDomElement element = baza.createElement("listItem");
element.setAttribute("task", data);
korzen.appendChild(element);
}
void Test::save(){
if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
QTextStream stream(&file);
stream << baza.toString();
file.close();
}
}
QString Test::sciezka() {
return (QDir::homePath()+"/items.xml");
}
02-16-2013 04:32 PM - edited 02-16-2013 04:35 PM
XMLDataModel items can't be changed once they are loaded. This model is very limited and is good only for prototyping things.
To reload ListView data while using XMLDataModel save the changes to disk then reset dataModel's source property. But this is very inefficient.
I think it's better to switch to ArrayDataModel or, if sorting is needed, to GroupDataModel. Parse XML manually using XMLDataAccess class and insert the data into GroupDataModel. This page has sample code for this:
http://developer.blackberry.com/cascades/documenta
This way you can modify dataModel's entries, add new ones etc and the changes will be automatically reflected in ListView. DataModel emits signals when data changes and ListView reacts to them.
It's also possible to create a custom DataModel subclass.
02-16-2013 04:56 PM
Thank ypu a lot for your quick reply.
But I am a beginer in Qt and especially in Cascades. I am writing my first app, so can you give me some solution or a little bit code example how use a GroupDataModel or ArrayDataModel to auto reload items (version for dummies please
)?