[英]Collapsible Panel in QML
我正在嘗試在QML中創建可折疊面板。 我面臨的問題是矩形( id: settingsBox
)的height
為0時:在這種情況下,我仍然可以看到包含的Label
( id: texter
)。 我希望當用戶單擊標題時,將對矩形高度進行動畫處理,然后顯示文本。 我該怎么辦?
Rectangle {
id: panel
property int margin: 5
property int minWidth: 200
property int prefWidth: 300
property int maxWidth: 500
property int minHeight: 300
property int prefHeight: 500
property int maxHeight: 600
property int minHeaderHeight: 30
property int prefHeaderHeight: 50
property int maxHeaderHeight: 50
property int maxPanelHeight: 600
property int duration: 50
width: 500
height: 500
ColumnLayout {
id: layout
anchors.fill: parent
HeaderButton {
id: headerButton1
height: prefHeight
anchors.left: parent.left
anchors.leftMargin: margin
anchors.right: parent.right
anchors.rightMargin: margin
anchors.top: parent.top
anchors.topMargin: margin
Layout.fillWidth: true
Layout.minimumWidth: minWidth
Layout.preferredWidth: prefWidth
Layout.maximumWidth: maxWidth
Layout.fillHeight: true
Layout.minimumHeight: minHeaderHeight
Layout.preferredHeight: prefHeaderHeight
Layout.maximumHeight: maxHeaderHeight
onHeaderBtnClicked: {
console.log("Settings Opened");
if(settingsBox.height===maxPanelHeight) {
heightAnimationRevert.start();
} else {
heightAnimation.start();
}
}
PropertyAnimation {
id: heightAnimation
target: settingsBox
property: "height";
from: 0;
to: maxPanelHeight;
duration: duration;
running: false;
loops: 1;
}
PropertyAnimation {
id: heightAnimationRevert
target: settingsBox
property: "height";
from: maxPanelHeight;
to: 0;
duration: duration;
running: false;
loops: 1;
}
}
Rectangle {
id: settingsBox
anchors.top: headerButton1.bottom
anchors.right: parent.right
anchors.leftMargin: margin
anchors.left: parent.left
anchors.rightMargin: margin
width: prefWidth
height: 0
color: "lightgray"
Label {
id: texter
visible: true
text: qsTr("Hello World");
font.pointSize: 11
clip: false
textFormat: Text.AutoText
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
}
}
我終於得到了答案。 這是使用Spliview並為splitView中每個子級的動畫高度。 即使孩子的身高為零,Spliview也會隱藏其孩子的內容。
@BaCaRoZzo提供的答案與我想要的差不多,但是當堆疊多個相似的組件時,我們會自行處理高度和其他問題。
我為有需要的人提供了代碼。 謝謝@BaCaRoZzo的回答。 我將在其他地方使用opacity屬性!
Rectangle {
id: rectangle2
width: 500
height: 600
SplitView {
anchors.fill: parent
orientation: Qt.Vertical
PropertyAnimation {
id: heightAnimation
target: rect1
property: "height";
from: 0;
to: 400;
duration: 500;
running: false;
loops: 1;
}
Rectangle {
id: rect1
height: 0
Layout.maximumHeight: 400
color: "blue"
Text {
text: "View 1"
anchors.centerIn: parent
}
}
Rectangle {
id: rect2
Layout.minimumHeight: 50
Layout.fillHeight: true
color: "lightgray"
Text {
text: "View 2"
anchors.centerIn: parent
}
}
Rectangle {
id: rect3
height: 200
color: "lightgreen"
Text {
text: "View 3"
anchors.centerIn: parent
}
MouseArea {
id: mouseArea1
x: 8
y: 5
width: 484
height: 187
onClicked: heightAnimation.start();
}
}
}
}
(最后一個矩形用作按鈕)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.