OJFord
OJFord

Reputation: 11130

Create dynamic object from string, with variables as anchor property values

In a function property of an element with id: window, I create dynamic objects as follows:

Qt.createQmlObject(
    'import QtQuick 2.3; Rectangle {color: "#00FF00"; width: 1; anchors.right: '+i+'; height:1; anchors.bottom: '+i+'; }',
    window, "dynamicPixels");

where i is the iterating variable of a loop containing this snippet.

When I attempt to do some calculation with i, and use that as the value in the dynamic object, it complains:

Unexpected token: 'numeric literal'

I tried Math.round() thinking it might somehow have become double (even though the calculation involved only ints), but without change.

However, when I reduced the line to just i to make my post here clearer, I get the different error:

Invalid property assignment: unsupported type "QQuickAnchorLine"

So do I have deeper problems - can I not use anchors at all in a dynamic object?

Upvotes: 1

Views: 1428

Answers (1)

Mitch
Mitch

Reputation: 24406

You want anchors.rightMargin. anchors.right is for anchoring to items by ID.

Upvotes: 4

Related Questions