gpt4 book ai didi

qt - 将元素动态添加到 QML 中的 SplitView

转载 作者:行者123 更新时间:2023-12-04 02:19:09 26 4
gpt4 key购买 nike

我正在使用 QML,我想动态地将元素添加到 SplitView,例如。 onMouseClick,但到目前为止我还没有找到答案。

到目前为止我发现的是 SplitView是否将其默认属性设置为其第一个 child 的 data属性(property)。所以我想我应该尝试添加新的动态创建的组件,并将父组件设置为该子组件( splitView1.children[0] )。不幸的是,这也不起作用。更重要的是,在组件完成加载后,第一个 child 的 child 数量为零(似乎 SplitLayout 的 Component.onCompleted 事件调用了一个将这些 child 移动到其他地方的函数)。因此,添加的子项不会呈现(并且不会响应任何 Layout 附加属性)。

请查看以下代码片段:

import QtQuick 2.1
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0

ApplicationWindow {
width: 600
height: 400

SplitView {
anchors.fill: parent

Rectangle {
id: column
width: 200
Layout.minimumWidth: 100
Layout.maximumWidth: 300
color: "lightsteelblue"
}

SplitView {
id: splitView1
orientation: Qt.Vertical
Layout.fillWidth: true

Rectangle {
id: row1
height: 200
color: "lightblue"
Layout.minimumHeight: 1
}

// Rectangle { //I want to add Rectangle to splitView1 like this one, but dynamicly eg.onMouseClick
// color: "blue"
// }
}
}

MouseArea {
id: clickArea
anchors.fill: parent
onClicked: {
console.debug("clicked!")
console.debug("len: " + splitView1.__contents.length); // __contents is the SplitView's default property - an alias to the first child's data property

var newObject = Qt.createQmlObject('import QtQuick 2.1; Rectangle {color: "blue"}',
splitView1, "dynamicSnippet1"); //no effect

// var newObject = Qt.createQmlObject('import QtQuick 2.1; import QtQuick.Layouts 1.0; Rectangle {color: "blue"; width: 50; height: 50}',
// splitView1, "dynamicSnippet1"); //rectangle visible, but not in layout(?) - not resizeable
}
}
}

有什么方法可以让动态创建的组件在 SplitView 中正确呈现作为静态添加的?

最佳答案

该 API 似乎不支持动态插入新元素。即使你确实让它工作了,它也会是一个黑客,并且可能会在 future 的版本中中断。您可能需要滚动自己的控件来模仿您想要的行为。理想情况下,它应该得到某种模型的支持。

关于qt - 将元素动态添加到 QML 中的 SplitView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17923811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com