01-05-2013 07:03 AM
Hello,
I'm building an application partially written with qml.
I 've made a qml page with a ListView and XmlDataModel.
All works if the xmldatamodel read the .xml source file from assets..
But if I change the source xml to point on a file produced in data directory, the log report that he cannot access the file..
Why?
The file is present and the content seems correct.
Should I have to enable something?
Thanks you
Solved! Go to Solution.
01-05-2013 10:05 AM
Can you post your code that shows how you are accessing the file?
01-05-2013 12:22 PM - edited 01-05-2013 12:22 PM
this is the function that write the file:
void MealList::saveTheList() {
FILE *f;
f = fopen("./data/yourmeallist.xml","w");
if(f==NULL) {
fprintf(stdout,"ERROR WRITING FILE! ");
return;
}
fprintf(f,"<root>\n");
for(std::vector<MealItem *>::iterator it = _meals->begin();it!= _meals->end();it++) {
QByteArray ba = (*it)->myName().toLatin1();
const char *c_str = ba.data();
fprintf(f,"\t<mealItem title=\"%s\" checked=\"%s\"></mealItem>\n",c_str,(*it)->isCheck
}
fprintf(f,"</root>\n");
fclose(f);
}
I did a "cat" command of the file from the console and it seems correct.
This is instead the qml page that read the file:
import bb.cascades 1.0
import "common"
Page {
content: MenuContainer {
id: lastList
objectName: "lastList"
// ======== Properties =============
// ======== SIGNAL()s ==============
// ======== SLOT()s ================
// ======== Local functions ========
Container {
layout: StackLayout {
}
topPadding: 10
leftPadding: 15
rightPadding: 30
Label {
text: "Smart List"
textStyle {
base: SystemDefaults.TextStyles.BigText
color: Color.LightGray
fontWeight: FontWeight.Bold
}
}
TextArea {
editable: false
text: "Products in your list:"
textStyle {
base: SystemDefaults.TextStyles.BodyText
color: Color.LightGray
lineHeight: 1.1
}
}
Container {
layout: DockLayout {
}
ListView {
dataModel: XmlDataModel {
source: "../../../data/yourmeallist.xml"
// previous working path"models/samplelist.xml"
}
listItemComponents: [
ListItemComponent {
type: "mealItem"
Container {
layout: StackLayout {
}
topPadding: 15
leftPadding: 20
CheckBox {
}
Label {
} // end of Container
} // end of second ListItemComponent
]
onCreationCompleted: {
}
} //end of listview
}//end of container
} //end of first container
}// menu container
paneProperties: NavigationPaneProperties {
backButton: ActionItem {
title: "Pop"
onTriggered: {
console.log("popped: have you saved the state?");
pop();
}
}
}
}
Thanks you
01-07-2013 02:32 AM - edited 01-07-2013 02:33 AM
Ok it was a path problem.. the path source: "../../../data/yourmeallist.xml" was wrong.
I didn't know how to pass the correct reference to application data directory.. so the solution I've found , it is to pass the PATH of the file to QML as a ContextProperty.. like this:
QDeclarativePropertyMap* propertyMap = newQDeclarativePropertyMap;
propertyMap->insert("XMLPATH",QVariant(QString(QDir::homePath() + "/yourmeallist.xml")));
_qml->setContextProperty("_propertyMap", propertyMap);
and than in qml read that property
....
ListView {
dataModel: XmlDataModel {
source: _propertyMap.XMLPATH
}
...
Not the best solution.. but it works
![]()