QML在不重新加载内容的情况下更改Pathview的路径

时间:2017-05-04 17:47:18

标签: qml qtquick2

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

有没有办法在不重新加载内容的情况下做到这一点,或者可能会覆盖'眨眼效应?

示例:

states <- str_extract(text, "\\b[A-Z]{2}(?=\\s+\\d{4,5}$)")

2 个答案:

答案 0 :(得分:1)

PathView似乎使用了这个技巧,在Path发生变化后强制重新布局。我发现没有理想的方法来做这个,但是要阻止PathView摧毁你的代表,可以使用中间DelegateModel来完成。

DelegateModelItem实例化了view,您可以通过将Item添加到persistedItems来选择让Item持久化-group。

由于我们可能希望使用模型的动态实例化,对于此示例,我只将那些Path添加到此组,实例化Item将切换,并删除他们在转换后立即离开小组。

正如我所说的那样:我没有找到(但是看起来并不太多)一种强制重新布局的好方法。所以暂时我会稍微移动一下视图,否则代表的x和y值不会被更新。

如果您的所有persistent都可见,您可以在Component.onCompleted - 广告位中将所有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 } } } 标记为command

shell

答案 1 :(得分:0)

我不知道它是否只是上述测试用例中的路径,或者是否可以使用真实应用中的路径,但一个选项可能是更改路径的属性而不是更改整条道路。看起来你甚至可以设置路径属性的动画:

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
    }
}
相关问题