关于QT中QML stackView 页面重复push的解决方法
我在学习qml的时候碰见一个问题,那就是使用stackView push页面会出现重复push的情况,这种问题可以通过stackView中的find函数来解决,通过find查栈中这个页面是否存在,存在就不再push,不存在则push,这样就能解决页面重复push的问题。
QT官网的实例
Item find(callback, behavior)
Search for a specific item inside the stack. function will be called for each item in the stack (with the item as argument) until the function returns true. Return value will be the item found. For example: find(function(item, index) { return item.isTheOne }) Set onlySearchLoadedItems to true to not load items that are not loaded into memory
stackView.find(function(item, index) {
return item.isTheOne
})
stackView.pop(stackView.find(function(item) {
return item.name == "order_id";
}));
find函数查找时候,成功找到就返回Item,找不到就返回null。
第一个参数是一个回调函数,该回调函数被调用堆栈中的每个项的项目和索引作为参数),直到回调函数返回 true。
第二个参数是行为。
下面是我的实例代码。
先在你的页面中设置一个属性,类型为string或者bool都可以,根据自己的需要设置。
属性代码.
Item {
id:root
width: 1920
height: 1280
property string only_id: "onlyName"
}
处理函数代码.
function{
var findItem = stack.find(function(item,index){
//console.log(item)
//console.log(index)
console.log(item.only_id);
//console.log("bool = ",item.only_id=== "onlyName" ? true : false)
return item.only_id === "onlyName" ? true : false
})
//onsole.log("findItem=",findItem)
if(findItem === null){
console.log("stack push page!!!")
stack.push("Page.qml",{})
}
else
{
console.log("this page is existed ")
}
}
本文介绍了在QT QML中遇到StackView页面重复push的问题及其解决方法。通过利用StackView的find函数检查页面是否已存在于栈中,避免了重复push。示例代码展示了如何设置回调函数来判断页面是否存在,并提供了实际应用的代码片段,确保只有当页面不存在时才进行push操作。
615





