[Qt-qml] Views and models

bea.lam at nokia.com bea.lam at nokia.com
Mon Aug 9 08:59:49 CEST 2010


Hi Cornelius,

On 07/08/2010, at 1:14 AM, ext Cornelius Hald wrote:
> [...]
> 
> So far, so good. I will get all sides from my first slide show. But how 
> can I, for example, access the <name> element of <slideshow>? And how 
> can I access the other slide shows?
> 
> I would like to have a list, where each element looks like this:
> * slide show name: /root/slideshow/name/string()
> * slide text:      /root/slideshow/slide/text/string()
> * slide image:     /root/slideshow/slide/image/string()
> * More...
> 
> I tried adding a XmlRole that accesses the parent element like:
> 
> XmlRole { name: "parentName"; query: "../name/string()" }
> 


It looks like you should start the query from "/root/slideshow" rather than "/root/slideshow/slide", and fetch the slideshow name using 

        XmlRole { name: "name"; query: "name/string()" }

To access each slide within a slideshow, you can create an inner XmlListModel that queries "/root/slideshow/slide" as you did before, but access a specific slide by its index, e.g. "root/slideshow/slide[1]" (see http://doc.qt.nokia.com/4.7-snapshot/qml-xmlrole.html). For example,

    ListView {
	width: 300; height: 400
        model: slideshow
        delegate: Column {
            Text { text: name }

            ListView {
                x: 50
                width: 300; height: childrenRect.height
                model: XmlListModel {
                    source: "slideshow.xml"
                    query: "/root/slideshow[" + (index + 1) + "]/slide"

                    XmlRole { name: "image"; query: "image/string()" }
                    XmlRole { name: "audio"; query: "audio/string()" }
                }
                delegate: Column {
                    Text { text: "* Slide " + (index + 1) + ":" } 
                    Text { text: image }
                    Text { text: audio }
                }
            }
        }
    }

XmlListModel is focused on creating a model from xml data rather than fetching a single xml value. In a later release we'll look to improve support in this area as well.


regards,

Bea



More information about the Qt-qml mailing list