QML更改路径视图';在不重新加载内容的情况下删除路径
我正面临Pathview的问题。我需要更改path属性来重新组织子项,但在执行此操作时,会导致所有已创建的元素(由模型指定)被销毁并再次创建 有没有办法做到这一点而不重新加载内容,或者“掩盖”眨眼效果 例如:QML更改路径视图';在不重新加载内容的情况下删除路径,qml,qtquick2,Qml,Qtquick2,我正面临Pathview的问题。我需要更改path属性来重新组织子项,但在执行此操作时,会导致所有已创建的元素(由模型指定)被销毁并再次创建 有没有办法做到这一点而不重新加载内容,或者“掩盖”眨眼效果 例如: import QtQuick 2.6 import QtQuick.Window 2.2 import QtQuick.Controls 1.2 Window { visible: true width: 640 height: 480 title: qs
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
}
}
我不知道这是否只是上面测试用例中的路径,或者你在真实应用程序中的路径是否可行,但一个选择是更改路径的属性,而不是更改整个路径。看起来您甚至可以设置路径属性的动画:
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
}
}
PathView
似乎使用了这个技巧,在路径更改后强制重新布局。目前为止,我还没有找到理想的方法来实现这一点,但是要阻止路径视图
破坏您的代理,可以使用中间代理模型
来实现
DelegateModel
为视图
实例化项
s,您可以选择将项
s持久化,方法是将它们添加到persistedems
-组中
由于我们可能希望使用模型的动态实例化,因此在本例中,我仅向该组添加那些在路径
将切换时实例化的项
,并在切换后将其从组中移除
正如我所说:我并没有找到(但并没有寻找太多)一个很好的方法来迫使重新发布。所以现在,我需要稍微移动视图,否则代理的x和y值将不会更新
如果您的项目仍然可见,您可以在组件.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
}
}
}
呵呵,这是我首先想到的,但我更改了path1
中的属性,并使用了path2
,因此我没有得到任何结果:DIt正在工作,但现在,我无法从我的新委托访问PathAttribute定义的附加属性,我该如何做?编辑:现在一切正常,我必须将所有附加属性定义为每个路径中的默认值,因为不重新加载委托绑定不起作用。谢谢你的帮助:)