Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
crazycoder1999
Posts: 14
Registered: ‎12-06-2012
My Device: Dev Alpha
My Carrier: Vodafone
Accepted Solution

XmlDataModel and qml: read model from data directory

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

 

Please use plain text.
Developer
zezke
Posts: 1,004
Registered: ‎12-12-2010
My Device: Z10 (red Limited Edition)
My Carrier: Mobile Vikings

Re: XmlDataModel and qml: read model from data directory

Can you post your code that shows how you are accessing the file?

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Please use plain text.
Contributor
crazycoder1999
Posts: 14
Registered: ‎12-06-2012
My Device: Dev Alpha
My Carrier: Vodafone

Re: XmlDataModel and qml: read model from data directory

[ Edited ]

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)->isChecked()?"true":"false");
    }
    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 {
                                                           orientation: LayoutOrientation.LeftToRight  
                                                   }
                                                   topPadding: 15
                                                   leftPadding: 20
                                                   CheckBox {
                                                           // Determine whether the CheckBox should be checked
                                                           // according to a value in the data model
                                                           checked: ListItemData.checked
                                                
                                                   }
                                                   Label {
                                                           text: ListItemData.title
                                                                     
                                                           // Apply a text style to create a title-sized font
                                                           // with normal weight
                                                           textStyle {
                                                                   base: SystemDefaults.TextStyles.TitleText
                                                                   fontWeight: FontWeight.Normal
                                                                   
                                                                   //fontSize: FontSize.XLarge;
                                                                   color: Color.White;
                                                                   
                                                                   }
                                                           }
                                           } // 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

Please use plain text.
Contributor
crazycoder1999
Posts: 14
Registered: ‎12-06-2012
My Device: Dev Alpha
My Carrier: Vodafone

Re: XmlDataModel and qml: read model from data directory

[ Edited ]

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 

:smileywink:

Please use plain text.