关于QT中QML stackView 页面重复push的解决方法

本文介绍了在QT QML中遇到StackView页面重复push的问题及其解决方法。通过利用StackView的find函数检查页面是否已存在于栈中,避免了重复push。示例代码展示了如何设置回调函数来判断页面是否存在,并提供了实际应用的代码片段,确保只有当页面不存在时才进行push操作。

关于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 ")
          }
}
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值