[Qt-qml] Dynamically created items and properties
Jan Ekholm
jan.ekholm at smultron.net
Mon Jul 26 09:41:34 CEST 2010
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 "scenario.units.units[index]" maps to a real C++ Unit* and that code has
worked fine in several other contexts. Still, no go. The item just does not
react to property changes.
--
Pets are always a great help in times of stress. And in times of
starvation too, o'course.
-- Terry Pratchett, Small Gods
More information about the Qt-qml
mailing list