【问题标题】:Add elements dynamically to SplitView in QML将元素动态添加到 QML 中的 SplitView
【发布时间】:2013-07-29 12:17:32
【问题描述】:

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

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

请看以下代码sn-p:

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 中像静态添加的组件一样正确呈现?

【问题讨论】:

    标签: qt qml qt-quick qtquick2


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      从 QtQuick Controls 1.3 开始,SplitView 有一个 addItem(item) method

      【讨论】:

        【解决方案3】:

        你必须使用加载器来加载动态对象。在 onClicked 句柄中,您必须声明 sourceComponent 属性以更改加载器的源,如下所示:

        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
                }
             Loader {
                 id:rect
             }
        
        
            }
        }
        
        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
                rect.sourceComponent = algo
            }
        }
        
        Component {
            id:algo
        Rectangle {
            anchors.fill: parent
            color: "blue"
        }
        }
        }
        

        【讨论】:

        • 这是一个非常糟糕的解决方案,因为您必须提前知道需要多少个对象,这种情况很少发生。
        【解决方案4】:

        我看到了SplitView的源代码,它在Component.onCompleted信号时计算每个分割区域。所以我认为这是一个关键点。不管你怎么做(插入、动态创建)。插入新区域进行拆分后,该区域不会重置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-06-15
          • 1970-01-01
          • 1970-01-01
          • 2016-02-09
          • 2014-04-13
          • 2017-09-26
          • 1970-01-01
          • 2011-07-05
          相关资源
          最近更新 更多