[Qt-qml] Dynamically created items and properties
Martin Jones
martin.jones at nokia.com
Tue Jul 27 00:42:36 CEST 2010
On Mon, 26 Jul 2010 05:41:34 pm ext Jan Ekholm wrote:
> I tried to change the Unit item to really, really bind to the C++
> properties
>
> using this change:
> > Item {
> >
> > function createUnits (component) {
> >
> > if ( component.status == Component.Ready ) {
> >
> > // get all units, this is a C++ list of Unit*
> > var unit_list = scenario.units.units;
> >
> > for ( var index = 0; index < unit_list.length; index++ ) {
> >
> > // the c++ unit, a Unit*
> > var unit = unit_list[index]
> >
> > // create a new item with the root Item as parent, see
> > // the "id" property
> > var item = component.createObject( units );
> >
> > if (item == null) {
> >
> > console.log( "Units.createUnits: failed to create
> > unit"
> >
> > ); }
> >
> > else {
>
> item.setup( index )
>
> > }
> >
> > }
> >
> > }
> >
> > else if (component.status == Component.Error) {
> >
> > // ugh
> >
> > }
> >
> > }
> >
> > function loadComponent () {
> >
> > // load the component
> > var component = Qt.createComponent("Unit.qml");
> >
> > // create the real items
> > createUnits( component );
> >
> > }
> >
> > Component.onCompleted: loadComponent();
> > id: units
> >
> > }
>
> And the Unit.qml file would have this function to perform the binding:
>
> Image {
>
> function setup (index) {
> x = scenario.units.units[index].x
> y = scenario.units.units[index].y
> source = scenario.units.units[index].icon
> unit_id = scenario.units.units[index].id
> }
>
> property int unit_id
> ...
> }
The above is not a binding. It is an assignment. Bindings are created using
the colon syntax.
One solution is to provide the Unit to the QML item and let it setup the
bindings:
Unit.qml:
Image {
property variant unit
x: unit.x * 48 + ( unit.y % 2 ) * 24
y: unit.y*36
source: unit.icon
unit_id: unit.id
}
Then you just need to assign the unit to the QML item and it will take care of
its bindings.
--
Martin
More information about the Qt-qml
mailing list