admin管理员组文章数量:1356566
I have dynamically created some QML rectangles on a Canvas. I wish to toggle whether the rectangles can be dragged around or not via the drag.target property, which is declared upon creation inside MouseArea. If the rectangles are draggable, I set drag.target to "parent". If not, I set it to "undefined".
The rectangles are stored in an array called verts[]. When I try to access the drag.target property, I get an error: TypeError: Value is undefined and could not be converted to an object
Here is the function that produces the error. I am feeding it an array of rectangles (verts) 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";
}
}
}
I have tried feeding it the values of "parent" and "undefined" in quotes, without quotes, by setting another variable to undefined and passing that variable, and an empty string. Perhaps there are values defined in QML somewhere, such as QtQuick.(SomeClassOfEnums).undefined
But I suspect that the bigger problem is that it is not recognizing vert[i].drag.target as a property. Perhaps my path is wrong. I have tried
vert[i].drag.target
vert[i].MouseArea.drag.target
vert[i].Rectangle.MouseArea.drag.target
vert[i].Rectangle.drag.target
Additionally, I am able to set the color of those rectangles via javascript. The same syntax allows me to set the color without any error:
function setVertsColor(verts, color) {
for (var i=0;i<verts.length; i++) {
verts[i].color = color;
}
}
So if this syntax works with color, what is the problem with the drag.target property?
If it helps, here is the command I use to dynamically create the rectangles:
var component = Qt.createComponent("Vertex.qml");
var vertex = component.createObject(parent, {x: xPos, y: yPos, width: size, color:"yellow"});
And here is the Vertex.qml which contains the definition of the rectangles, called by component.createObject(...)
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 have any ideas. I'm fresh out of 'em.
I have dynamically created some QML rectangles on a Canvas. I wish to toggle whether the rectangles can be dragged around or not via the drag.target property, which is declared upon creation inside MouseArea. If the rectangles are draggable, I set drag.target to "parent". If not, I set it to "undefined".
The rectangles are stored in an array called verts[]. When I try to access the drag.target property, I get an error: TypeError: Value is undefined and could not be converted to an object
Here is the function that produces the error. I am feeding it an array of rectangles (verts) 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";
}
}
}
I have tried feeding it the values of "parent" and "undefined" in quotes, without quotes, by setting another variable to undefined and passing that variable, and an empty string. Perhaps there are values defined in QML somewhere, such as QtQuick.(SomeClassOfEnums).undefined
But I suspect that the bigger problem is that it is not recognizing vert[i].drag.target as a property. Perhaps my path is wrong. I have tried
vert[i].drag.target
vert[i].MouseArea.drag.target
vert[i].Rectangle.MouseArea.drag.target
vert[i].Rectangle.drag.target
Additionally, I am able to set the color of those rectangles via javascript. The same syntax allows me to set the color without any error:
function setVertsColor(verts, color) {
for (var i=0;i<verts.length; i++) {
verts[i].color = color;
}
}
So if this syntax works with color, what is the problem with the drag.target property?
If it helps, here is the command I use to dynamically create the rectangles:
var component = Qt.createComponent("Vertex.qml");
var vertex = component.createObject(parent, {x: xPos, y: yPos, width: size, color:"yellow"});
And here is the Vertex.qml which contains the definition of the rectangles, called by component.createObject(...)
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 have any ideas. I'm fresh out of 'em.
Share Improve this question edited Apr 1 at 23:29 eyllanesc 245k19 gold badges201 silver badges280 bronze badges asked Mar 28 at 0:09 brianscbriansc 32 bronze badges1 Answer
Reset to default 0You can use a delegate/model to dynamically create and destroy components. You can also replace MouseArea with a DragHandler and TapHandler. By adding dragEnabled to your model we can control runtime dragability via property binding:
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
Page {
property list<var> pts: []
Repeater {
model: pts
delegate: Vertex {
x: modelData.x
y: modelData.y
dragEnabled: modelData.dragEnabled
}
}
function addRandom(n) {
do {
let x = Math.floor(Math.random() * 300);
let y = Math.floor(Math.random() * 300);
let dragEnabled = Math.random() >= 0.5;
pts.push( {x,y,dragEnabled} );
} while (--n > 0);
}
Component.onCompleted: addRandom(3)
footer: Frame {
RowLayout {
Button { text: "Clear"; onClicked: pts.length = 0 }
Button { text: "Add 1"; onClicked: addRandom() }
Button { text: "Add 10"; onClicked: addRandom(10) }
}
}
}
// Vertex.qml
import QtQuick
import QtQuick.Controls
Rectangle {
width: 16
height: 16
color: dragEnabled ? "green" : "red"
property bool dragEnabled: true
DragHandler { enabled: dragEnabled }
TapHandler { onTapped: dragEnabled = !dragEnabled }
}
You can Try it Online!
本文标签: qtSetting dynamically created QML rectangle39s dragtarget property via JavaScriptStack Overflow
版权声明:本文标题:qt - Setting dynamically created QML rectangle's drag.target property via JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744062600a2584401.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论