当currentIndex === -1时如何让StackLayout折叠?

问题描述 投票:0回答:1

下面的代码只是做了一个简单的事情:点击时将

currentIndex
StackLayout
更改为
-1
。但我想让当
StackLayout
0
时,
currentIndex
的高度为
-1

Window {
    id: window
    width: 400
    height: 400
    visible: true
    color: "#181818"
    MouseArea {
        id: ma
        anchors.fill: parent
        hoverEnabled: true
        onClicked: lay.currentIndex = -1
    }

    StackLayout {
        id: lay
        anchors.top: parent.top
        Rectangle {
            width: 300
            height: 100
            color: "white"
        }
    }
    Rectangle {
        anchors.top: lay.bottom
        width: 200
        height: 100
        color: "red"
    }
}

我尝试过像这样的绑定

Binding {
    when: lay.currentIndex === -1
    lay.height: 0
}

但这行不通。

qt qml qtquick2
1个回答
0
投票

您可以收听内置

onCurrentIndexChanged
信号

import QtQuick 2.2
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12

Window {
    id: window
    width: 400
    height: 400
    visible: true
    color: "#181818"
    MouseArea {
        id: ma
        anchors.fill: parent
        hoverEnabled: true
        onClicked:lay.currentIndex = -1
    }

    StackLayout {
        id: lay
        anchors.top: parent.top
        Rectangle {
            width: 300
            height: 100
            color: "white"
        }
        onCurrentIndexChanged: {
            if (lay.curentIndex === -1)
                lay.height = 0
        }
    }
    Rectangle {
        anchors.top: lay.bottom
        width: 200
        height: 100
        color: "red"
    }
}

刚刚在 Qt 5.12.8 上测试过

© www.soinside.com 2019 - 2024. All rights reserved.