QML QtQuick.动态创建的组件不适用于绑定和基本方法



Env: OS - win 10 x86, qtcreator 4.6.1, mingw 5.3, qt 5.11

使用矩形创建 rect1.qml 文件。

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_bounding_box - 在生成的实例 (active_object( 中,将不会调用信号处理程序。 如果我使用 args Qt.point(active_object.x, active_object.y( 调用active_object的"包含"方法,则没有任何反应,就好像调用存根一样

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 ()
}

active_objectis中的边框颜色是"绿色",类似于矩形颜色。

这是一个错误或我在 qml 中不明白的东西?

"contains(point("方法显然不起作用,因为"可调用"和"虚拟"。 动态创建对象的自动绑定不起作用。必须对所选属性执行手动绑定。

最新更新