Setting a QML property to undefined via JavaScript
-
I created a bunch of rectangles dynamically, and I want the user to be able to drag them around, but they should be “locked” when the system is in a particular mode. To do this, I want to toggle between settings in the MouseArea:
drop.target: undefined
drop.target: parentI have all of my rectangles (referred to as verts) in an array, and I want to be able to iterate through them and set them to either “parent” or “undefined”.
vert[i].drop.target="undefined"
I am able to do this with color, by passing it a string.
vert[i].color=“yellow”But it does not like the term “undefined” (I'm not sure if it's ok with "parent" yet or not -- it doesn't error, but I don't know yet if it's doing the right thing). If I try passing them in quotes, as a string, it gives the error:
TypeError: Value is undefined and could not be converted to an objectIf I remove the quotes, I get the same error. I have also tried:
drop.target: null
drop.target: “”I even tried the combination of:
readonly property var notDefined: undefined
verts[i].drag.target = notDefined;Same error.
I get the feeling I need some sort of object or enum that means “parent” or “undefined”. Something like…
QtQuick.<EnumClass>.parent or
QtQuick.<EnumClass>.undefined
But I can’t find that anywhere. Can somebody point me to it?Here's the code that I'm using. I feed it the array and a boolean.
function setVertsDraggable(verts, draggable) { for (var i=0; i<verts.length; i++) { if (draggable) { verts[i].drag.target = "parent"; } else { verts[i].drag.target = "undefined"; } } }
Thanks,
bc -
After poking around some more, I think the error message may be referring to the vert[i] object as being undefined, not the value I'm trying to feed it.
But my vert object works otherwise, and I can change the color on it using similar syntax, so it should be valid. Also, when I do a typeof(vert[i]), I get something along the lines of Vertex_QMLTYPE_3(0x6000003b82a0)
I get the same thing if I say console.log(vert[i]). That suggests to me that my object is valid.The verts are created dynamically, and they are created from the following .qml file, using these commands:
var component = Qt.createComponent("Vertex.qml");
var vertex = component.createObject(parent, {x: xPos, y: yPos, width: size, color:"yellow"});import QtQuick Rectangle { width: 8 height: 8 color: yellow property string tag: "" MouseArea { anchors.fill: parent drag.target: undefined drag.smoothed: false onReleased: { polyCanvas.requestPaint() } } }
Please let me know if you've got something...
bc