5

我有一个容器,它在运行时被动态创建的组件填充。这些元素中的每一个(我们称它们为DynamicObjects)都有一个子元素树(Nodes),也是动态创建的。更重要的是,Node组件可以相互嵌套(如树结构)。

让容器将id参数设置为"main_container"并且每个容器都将DynamicObject其设置为"dynamic_object"

每当我尝试main_container从任何嵌套Node元素中解决问题时,它都可以正常工作。问题是当我尝试dynamic_object从根以外的任何Node其他人(那个是 的直接后代/孩子dynamic_object)中解决时。结果是:

ReferenceError:动态对象未定义

我的问题是:这种行为背后的原因可能是什么?它与这些对象是动态创建的事实有关吗?(这是我的第一个想法,因为我总是可以解决它,main_container并且它是在 qml 代码中静态声明的)。

代码示例:(如果有任何遗漏,请在评论中告诉我)

// Container.qml

import "container_logic.js" as Logic

Rectangle {
  id: main_container

  Keys.onTabPressed: {
    Logic.createComponent("DynamicObject.qml", {/* some parameters */});
    var dynamic_object = Logic.object;

    Logic.createComponent("Node.qml",{labelText: "asdefg"}, undefined, dynamic_object)
    var asdefg = Logic.object;
    Logic.createComponent("Node.qml",{labelText: "tree A"}, undefined, dynamic_object)
    var tree_a = Logic.object;

    Logic.createComponent("Node.qml",{labelText: "a"}, undefined, asdefg)
    var a = Logic.object;
    Logic.createComponent("Node.qml",{labelText: "s"}, undefined, asdefg)
    var s = Logic.object;

    asdefg.subnodes = [a, s]

    Logic.createComponent("Node.qml",{labelText: "tree B", isInput: false}, undefined, dynamic_object)
    var tree_b = Logic.object;
    Logic.createComponent("Node.qml",{labelText: "xyz", isInput: false}, undefined, dynamic_object)
    var xyz = Logic.object;

    Logic.createComponent("Node.qml",{labelText: "x", isInput: false}, undefined, xyz)
    var x = Logic.object;
    Logic.createComponent("Node.qml",{labelText: "y", isInput: false}, undefined, xyz)
    var y = Logic.object;
    Logic.createComponent("Node.qml",{labelText: "z", isInput: false}, undefined, xyz)
    var z = Logic.object;

    xyz.subnodes = [x,y,z]

    dynamic_object.treeLeft = [asdefg, tree_a]
    dynamic_object.treeRight = [tree_b, xyz]
  }
}

// DynamicObject.qml

Rectangle {
  id: dynamic_object

  property alias treeLeft : tree_left.subnodes
  property alias treeRight: tree_right.subnodes

  Rectangle {
    id: content_area

    Node {
      id: tree_left

      labelText: "left"

      anchors.left: parent.left
    }

    Node {
      id: tree_right

      labelText: "right"

      anchors.right: parent.right
    }
  }
}

// Node.qml

ColumnLayout {
  id: node

  default property alias subnodes: subnodes_area.data
  property alias labelText: label.text

  Rectangle {
    id: header_area

    Text {
      id: label
    }

    MouseArea {
      id: mouse_area

      anchors.fill: parent

      hoverEnabled: true
      onHoveredChanged: {
        console.debug(main_container) // works fine
        console.debug(dynamic_object) // **generates the error for all nodes but the root one**
      }
    }
  }

  ColumnLayout {
    id: subnodes_area

    anchors.top: header_area.bottom
  }
}

// container_logic

var component = null
var owner = main_container
var object = null
var data = null

function createComponent(type, info, callback, container) {
  callback = callback || finishComponent
  owner = container || main_container

  if(component != null) {
    console.log("Error: a component is being loaded at this time");
    return;
  }

  component = Qt.createComponent(type)
  data = info

  if(component.status === Component.Ready) {
    callback()
  } else if(component.status === Component.Loading) {
    component.statusChanged.connect(callback)
  } else {
    console.log("Error loading component:", component.errorString())
  }
}

function finishComponent() {
  if(component.status === Component.Ready) {
    object = component.createObject(owner, data)
    if(object === null) {
      console.log("Error creating object")
    }
  } else if(component.status === Component.Error) {
    console.log("Error loading component:", component.errorString())
  }
  resetData()
}

function resetData() {
  component = null;
  data = null;
}
4

1 回答 1

4

根据http://qt-project.org/doc/qt-4.8/qdeclarativedynamicobjects.html

  • 如果使用 Qt.createComponent(),则创建上下文是调用此方法的 QDeclarativeContext
  • 如果定义了 Component{} 项并在该项上调用了 createObject(),则创建上下文是定义 Component 的上下文

问题是createComponent()每个后续的函数Node都是从 的上下文中调用的main_container,从而阻止了后代能够解析dynamic_objectid。

通过将负责创建嵌套Nodes 的代码移动到Node.qml文件中来解决它(实际上是到该 qml 文档导入的 javascript 文件中,但结果是相同的)。

于 2013-08-13T11:49:37.333 回答