[SOLVED] Dynamical aliases or equivalent of C pointers in QML
-
Hi.
I need to do a computer graphics lab: drawing ellipses and circles with a different algorithms. My teacher requires different algorithms for circles and ellipses (of course, I can draw circle as an ellipse, but the teacher is not satisfied with this approach). So I decided to place properties "circle" and "ellipse" for active figure into root object:
@
property var circle: ({ x: 256, y: 256, r: 64 })
property var ellipse: ({ x: 256, y: 256, width: 64, height: 128 })
@
Of course circle and ellipse have common properties (x, y) and common functions in which they participate (drawWithQtDefaultFunction, drawWithBresenhamMethod, ...). But they are not completely identical, so I cannot just write "ellipse" everywhere. I think it is logical to have pointer (alias, reference) to active figure (figure -> circle or figure -> ellipse) to avoid plenty of code like this:
@
TextField
{
id: edtX
text: (isCircle? circle : ellipse).x
validator: DoubleValidator { }
onTextChanged: (isCircle? circle : ellipse).x = parseFloat(text)
}
@
It can be simplified a little bit with additional function:
@
function activeFigure()
{
return isCircle? circle : ellipse;
}
@
but I don't like code appearance (parentheses confuses me):
@
text: activeFigure().x
@I've tried to use property aliases but (as I understood):
They can be applied only to QML objects (not to property var (i.e. JavaScript object)). So I have to have wrapper like:
@
QtObject
{
id: circle
property var obj: ({ x: 256, y: 256, r: 64 })
}
@
that isn't convenient.Even with wrappers I cannot change aliases. I got:
@QML PropertyChanges: Cannot assign to read-only property "b"@
here is simple code for test:
@
import QtQuick 2.0Rectangle
{
id: obj_rootQtObject { id: obj_x property string str: "x" } QtObject { id: obj_y property string str: "y" } property alias b: obj_x states: [ State { name: "state_x" PropertyChanges { target: obj_root b: obj_x } }, State { name: "state_y" PropertyChanges { target: obj_root b: obj_y } } ] state: "state_x" Text { text: b.str anchors.centerIn: parent } MouseArea { anchors.fill: parent onClicked: { parent.state = parent.state == "state_x"? "state_y" : "state_x" } }
}
@So I decided to use function with condition, but I don't really like it.
Are there better options?
-
OK, got it. I forgot that JavaScript assignment is not a data, but a reference copy (I'm a complete novice in JavaScript, only 3 days). So I just needed to make assignment on checkbox checked:
@
property var circle: ({ x: 256, y: 256, r: 64 })
property var ellipse: ({ x: 256, y: 256, width: 64, height: 128 })
property var figure: ellipse
...
CheckBox
{
id: chkCircle
text: "Circle"
checked: false
onCheckedChanged: figure = checked? circle : ellipse;
}
...
figure.x = parseFloat(text);
@This concrete problem is solved, but I still need dynamic aliases and full bindings between QML objects and JavaScript objects.