【问题标题】:QML QtQuick. Dynamically created component does not work with bindings and basic methodsQML QtQuick。动态创建的组件不适用于绑定和基本方法
【发布时间】:2018-12-06 21:59:21
【问题描述】:

环境:操作系统 - win 10 x86、qtcreator 4.6.1、mingw 5.3、qt 5.11

使用 Rectangle 创建 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 的“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 ()
            }

active_objectis 中的边框颜色为“绿色”,类似于矩形颜色。

这是一个错误还是我在 qml 中看不懂?

【问题讨论】:

  • 我不确定,是否在这里创建绑定:component.createObject(root, { "view_bounding_box": root.view_bounding_box }) - 我现在无法测试它,但我想用 JS 创建绑定,你需要调用 Qt.binding(function() { return root.view_bounding_box })跨度>
  • 你用参数调用loadComponent (base_containers[getRandom(base_containers.length - 1)])。您的函数声明不带参数? Logic 是什么?你在deactivate_objects 有什么?您是否验证过它的任何内容都包含这一点?
  • 我不知道,您要做什么,但您是否尝试过声明式/使用 QML 而不是 JS?
  • 是的,Qt.binding 是有效的,但在 qt 示例中你找不到它。 doc.qt.io/qt-5/qtqml-dynamicscene-content-itemcreation-js.html
  • 你调用loadComponent...为了方便,我剪了一段代码。

标签: qt qml components dynamically-generated


【解决方案1】:

“contains(point)”方法显然不起作用,因为“invocable”和“virtual”。 动态创建的对象的自动绑定不起作用。您必须对选定的属性进行手动绑定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-03
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    相关资源
    最近更新 更多