QML-QtQuick。动态创建的组件不适用于绑定和基本方法
Env:OS-win10x86,qtcreator 4.6.1,mingw5.3,qt5.11 使用矩形创建rect1.qml文件QML-QtQuick。动态创建的组件不适用于绑定和基本方法,qt,qml,components,dynamically-generated,Qt,Qml,Components,Dynamically Generated,Env:OS-win10x86,qtcreator 4.6.1,mingw5.3,qt5.11 使用矩形创建rect1.qml文件 import QtQuick 2.0 Rectangle { width: 40 height: 40; border.color: "blue" color: "green"; width: childrenRect.width height: childrenRect.height proper
import QtQuick 2.0
Rectangle {
width: 40
height: 40;
border.color: "blue"
color: "green";
width: childrenRect.width
height: childrenRect.height
property int sub_border: 1
property color item_color: "green"
property bool view_bounding_box: false
property int sub_border: 1
onView_bounding_boxChanged: {
root.border.width = root.view_bounding_box ? sub_border : 0
console.log("BaseContainer", root.view_bounding_box, root.border.color) }
}
在main.qml中动态创建组件
Rectangle {
id: root
border.color: "blue"
border.width: 0
focus: true
property int edge_len: 20
property bool view_bounding_box: false
property var active_object
property var deactivate_objects:[]
function loadComponent () {
var component = Qt.createComponent("rect1.qml");
if (component .status === Component.Ready) {
active_object = component.createObject(root, {
"view_bounding_box": root.view_bounding_box });
}else if (component.status === Component.Error) {
console.log("error creating component");
console.log(component.errorString());
}
}
如果我更改边界属性root.view\u bounding\u box-在生成的实例(活动对象)中,将不会调用信号处理程序。
如果我使用args Qt.point(active_object.x,active_object.y)调用active_object的“contains”方法,则不会发生任何事情,就像调用存根一样
var point = Qt.point(active_object.x, active_object.y)
for (var i = 0; i < deactivate_objects.length; i++) {
if (deactivate_objects[i].contains(point)) { // not worked
deactivate_objects.push(active_object)
loadComponent ()
return
}
}
if (!root.contains(point)) { // worked
deactivate_objects.push(active_object)
loadComponent ()
}
var point=Qt.point(active\u object.x,active\u object.y)
对于(var i=0;i
活动对象中的边框颜色为“绿色”,类似于矩形颜色
这是一个bug,或者我不理解qml中的某些内容?由于“invocable”和“virtual”,所以“contains(point)”方法显然不起作用。
动态创建对象的自动绑定不起作用。必须对所选属性执行手动绑定 我不确定您是否在这里创建绑定:
component.createObject(root,{“view\u bounding\u box”:root.view\u bounding\u box})
-我现在无法测试它,但我猜要使用JS创建绑定,您需要调用loadComponent(base\u containers[getRandom(base\u containers.length-1)])
带有一个参数。您的函数声明不接受参数?什么是逻辑
?您在停用\u对象中有什么?你验证过它的任何内容包含这一点吗?我不知道,你想做什么,但是你试过用QML而不是JS来做声明性的/吗?是的,Qt.binding是有效的,但是在Qt示例中你找不到它。您调用loadComponent。。。为了方便起见,我删去了一段代码。