QML 预创建项目以显示在 StackView 中 [英] QML precreate items to show in a StackView

查看:47
本文介绍了QML 预创建项目以显示在 StackView 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实例化项目,然后使用 StackView 显示它们,通过

MyItem0{编号:项目 0}我的项目 1{编号:项目 1}myStackView.push(item0)...myStackView.pop()myStackView.push(item1)

问题是我创建item0和item1的时候,都显示出来了,这显然不是我想要的.

我猜这与这篇文章有关.

有什么想法吗?

解决方案

在没有父级的情况下创建它们:

导入QtQuick 2.5导入 QtQuick.Controls 2.0导入 QtQuick.Window 2.0应用程序窗口{编号:窗口可见:真实宽度:600高度:600堆栈视图{anchors.fill:父级编号:sv初始项目:按钮{编号:bonClicked: sv.push(b1)文字:0"}}按钮 {编号:b1父级:空onClicked: sv.push(b2)文本1"×:100onParentChanged: console.log('parent', parent, 1)onVisibleChanged: console.log('visible',visible, 1)}按钮 {编号: b2父级:空onClicked: sv.push(b3)文字:2"×:200onParentChanged: console.log('parent', parent, 2)onVisibleChanged: console.log('visible',visible, 2)}按钮 {编号:b3父级:空onClicked: sv.pop()文字:3"×:300onParentChanged: console.log('parent', parent, 3)onVisibleChanged: console.log('visible', visible, 3)}按钮 {身份证:返回onClicked: sv.pop()文字:'流行'捆绑 {目标:background属性:'颜色'值:'橙色'}}按钮 {身份证:清除×:150onClicked: sv.clear()文字:'清除'捆绑 {目标:clear.background属性:'颜色'值:'橙色'}}}

QtQuick.Controls 2.x 中,当您 pop() 时,它们将被重新设置为 null,从而有效地隐藏它们.>

QtQuick.Controls 1.x 中,它们将被重新分配给某些 ContentItem,但它们的 visible 属性将设置为 false,也将它们隐藏起来.

要测试它,只需将第 2 行更改为 import QtQuick.Controls 1.4

记住: pop() 会将索引为 0 的项目留在堆栈中.要删除它,请调用 clear()

I want to instantiate items and then display them using a StackView, via

MyItem0
{
    id: item0
}

MyItem1
{
    id: item1
}

myStackView.push(item0)
...
myStackView.pop()
myStackView.push(item1)

The problem is that when I create items item0 and item1, they are all shown, which is obviously not what I want.

I guess it somehow relates to this post.

Any idea?

解决方案

Create them with no parent:

import QtQuick 2.5
import QtQuick.Controls 2.0
import QtQuick.Window 2.0

ApplicationWindow {
    id: window
    visible: true
    width: 600
    height: 600



    StackView {
        anchors.fill: parent
        id: sv
        initialItem: Button {
            id: b
            onClicked: sv.push(b1)
            text: "0"
        }
    }

    Button {
        id: b1
        parent: null
        onClicked: sv.push(b2)
        text: "1"
        x: 100
        onParentChanged: console.log('parent', parent, 1)
        onVisibleChanged: console.log('visible', visible, 1)
    }

    Button {
        id: b2
        parent: null
        onClicked: sv.push(b3)
        text: "2"
        x: 200
        onParentChanged: console.log('parent', parent, 2)
        onVisibleChanged: console.log('visible', visible, 2)
    }

    Button {
        id: b3
        parent: null
        onClicked: sv.pop()
        text: "3"
        x: 300
        onParentChanged: console.log('parent', parent, 3)
        onVisibleChanged: console.log('visible', visible, 3)
    }


    Button {
        id: back
        onClicked: sv.pop()
        text: 'pop'
        Binding {
            target: back.background
            property: 'color'
            value: 'orange'
        }
    }

    Button {
        id: clear
        x: 150
        onClicked: sv.clear()
        text: 'clear'
        Binding {
            target: clear.background
            property: 'color'
            value: 'orange'
        }
    }
}

In QtQuick.Controls 2.x the will be reparented to null when you pop() them, effectively hiding them.

In QtQuick.Controls 1.x they will be reparented to some ContentItem but their visible property will be set to false, hiding them aswell.

To test it, just change line 2 to import QtQuick.Controls 1.4

Remember: pop() will leave the item with index 0 on the stack. To remove that, call clear()

这篇关于QML 预创建项目以显示在 StackView 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆