[Qt-qml] Recommended way to parametrize QML items?

Tomas Junnonen tomas.junnonen at nokia.com
Wed Nov 17 14:59:11 CET 2010


On 11/17/2010 02:20 PM, Kellomaki Pertti (Nokia-MS/Tampere) wrote:
> What is the recommended way to parametrize QML items? What I would like
> to achieve is something the following. The intent is to create a Foo
> where one part of the visual appearance is given by the bar property.
>
>      Foo { bar: Text { text: "bar text" } }
>      Foo { bar: Image { source: "bar-image.png" } }
>
> Looking at SpinBox.qml in the custom branch of qt-components [1], I see
> that one way to do this is to use a Loader element as a place holder
> inside the definition of Foo, and set it up in the onLoaded handler.
> This is clear enough, though it does look like a bit roundabout way of
> doing things.

First try to use a simple direct property binding:

main.qml:
import Qt 4.7

Rectangle {
     width: 200; height: 200

     Foo { bar: Text { text: "bar text" } }
     Foo { bar: Image { source: "bar-image.png" } }
}

Foo.qml:
import Qt 4.7

Rectangle {
     property Item bar

     children: bar
}

The parametrized bar item doesn't have to be a direct child of Foo 
either. Another way is to use the onBarChanged signal to Do What You 
Want(tm), for example if you need to anchor the item.

Regards,
Tomas



More information about the Qt-qml mailing list