【问题标题】:QML changing Pathview's path without reloading contentQML 在不重新加载内容的情况下更改 Pathview 的路径
【发布时间】:2017-05-04 17:47:17
【问题描述】:

我正面临 Pathview 的问题。我需要更改路径属性来重新组织子项,但是当我这样做时,它会导致所有已创建的元素(由模型指定)被销毁并再次创建。

有没有办法在不重新加载内容的情况下做到这一点,或者“掩盖”眨眼效果?

例子:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("PathView path test")

    Path {
        id: path1
        startX: 100; startY: 100
        PathLine{ x: 300; y: 100 }
    }
    Path {
        id: path2
        startX: 100; startY: 100
        PathLine{ x: 100; y: 300 }
    }

    ListModel {
        id: pvModel
        ListElement{ name: "rectangle" }
        ListElement{ name: "rectangle" }
        ListElement{ name: "rectangle" }
    }

    Component {
        id: pvDelegate
        Rectangle {
            width: 50
            height: 50
            color: "red"
            border.width: 1
            Component.onCompleted: console.log("Rectangle created")
            Component.onDestruction: console.log("Rectangle deleted")
        }
    }

    property bool currentPath;
    PathView {
        anchors.fill: parent
        model: pvModel
        delegate: pvDelegate
        path: (currentPath ? path1 : path2)
    }

    Button {
        width: 100
        height: 40
        text: "Switch path"
        onClicked: currentPath = !currentPath
    }
}

【问题讨论】:

    标签: qt qml qtquick2


    【解决方案1】:

    PathView 似乎使用了这个技巧,在Path 更改后强制重新布局。到目前为止,我没有找到理想的方法来做到这一点,但是要阻止 PathView 破坏您的代表,可以使用中间 DelegateModel 来完成。

    DelegateModelview 实例化Items,您可以选择通过将Items 添加到persistedItems-group 中来保持Items 的持久性。

    由于我们可能想要使用模型的动态实例化,对于这个例子,我只将那些Items 添加到这个组中,当Path 将切换时,它们被实例化,并将它们从组中删除切换之后。

    正如我所说:我没有找到(但没有看太多)强制重新布局的好方法。所以目前,我会稍微移动一下视图,否则代表的 x 和 y 值将不会更新。

    如果您的所有Items 仍然可见,您可以在Component.onCompleted-slot 中将它们全部标记为persistent

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.2
    import QtQml.Models 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("PathView path test")
    
        Path {
            id: path1
            startX: 100; startY: 100
            PathLine{ id: line1; x: 300; y: 100 }
        }
        Path {
            id: path2
            startX: 100; startY: 100
            PathLine{ x: 100; y: 300 }
        }
    
        ListModel {
            id: pvModel
            ListElement{ name: "rectangle" }
            ListElement{ name: "rectangle" }
            ListElement{ name: "rectangle" }
        }
    
        DelegateModel {
            id: pvDelegateModel
            model: pvModel
            delegate: Rectangle {
                id: delegate
                width: 50
                height: 50
                color: 'red'
                border.width: 1
    
                Component.onCompleted: console.log("Rectangle created")
                Component.onDestruction: console.log("Rectangle destroyed")
                Connections {
                    target: button
                    onStart: delegate.DelegateModel.inPersistedItems = true // Make them persistent befor the switch 
                    onEnd: delegate.DelegateModel.inPersistedItems = false // Make them non-persistent after the switch
                }
            }
        }
    
        PathView {
            id: pv
            anchors.fill: parent
            model: pvDelegateModel
            path: path1
            clip: true
        }
    
        Button {
            id: button
            width: 100
            height: 40
            text: "Switch path"
            signal start
            signal end
            onClicked: {
                start()
                pv.path = (pv.path === path1 ? path2 : path1)
                end()
                pv.currentIndex +=1   // To force a refresh of the layout
                pv.currentIndex -= 1
            }
        }
    }
    

    【讨论】:

    • 它正在工作,但现在,我无法从我的新委托访问 PathAttribute 定义的附加属性,我该怎么做?
    • 编辑:现在一切正常,我必须将所有附加属性定义为每个路径中的默认值,因为不重新加载委托绑定不起作用。谢谢你的帮助:)
    【解决方案2】:

    我不知道这是否只是上述测试用例中的路径,或者您在实际应用中的路径是否可行,但一种选择可能是更改路径的属性而不是更改整个路径。看起来您甚至可以为路径属性设置动画:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("PathView path test")
    
        Path {
            id: pvPath
            startX: 100; startY: 100
            PathLine{
                x: currentPath ? 300 : 100
                y: currentPath ? 100 : 300
                Behavior on x { SmoothedAnimation { duration: 125 } }
                Behavior on y { SmoothedAnimation { duration: 125 } }
            }
        }
    
        ListModel {
            id: pvModel
            ListElement{ name: "rectangle" }
            ListElement{ name: "rectangle" }
            ListElement{ name: "rectangle" }
        }
    
        Component {
            id: pvDelegate
            Rectangle {
                width: 50
                height: 50
                color: "red"
                border.width: 1
                Component.onCompleted: console.log("Rectangle created")
                Component.onDestruction: console.log("Rectangle deleted")
            }
        }
    
        property bool currentPath;
        PathView {
            anchors.fill: parent
            model: pvModel
            delegate: pvDelegate
            path: pvPath
        }
    
        Button {
            width: 100
            height: 40
            text: "Switch path"
            onClicked: currentPath = !currentPath
        }
    }
    

    【讨论】:

    • 呵呵,这就是我最初的想法,但我更改了path1 中的属性并使用了path2,所以我没有得到任何结果:D
    猜你喜欢
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2014-01-10
    • 2015-02-24
    • 2021-01-10
    相关资源
    最近更新 更多