我是QML的新手,我製作了「手風琴」項目。我修改了一個不完整的QML項目。 對於這一點,我用PanelItems:如何在ColumnLayout中更改位置
Item {
default property var contentItem: null
property string title: "panel"
id: root
Layout.fillWidth: true
height: 30
Layout.fillHeight: current
property bool current: false
ColumnLayout {
anchors.fill: parent
spacing: 0
Rectangle {
Drag.active: dragArea.drag.active
id: bar
Layout.fillWidth: true
height: 30
color: root.current ? "#81BEF7" : "#CEECF5"
Text {
anchors.fill: parent
anchors.margins: 10
horizontalAlignment: Text.AlignLeft
verticalAlignment: Text.AlignVCenter
text: root.title
}
Text {
anchors{
right: parent.right
top: parent.top
bottom: parent.bottom
margins: 10
}
horizontalAlignment: Text.AlignRight
verticalAlignment: Text.AlignVCenter
text: "^"
rotation: root.current ? "180" : 0
}
MouseArea {
id: dragArea
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
drag.axis: Drag.YAxis
drag.target: root
onReleased: {
console.log("release !")
root.Drag.drop()
}
onClicked: {
if (root.parent.current !== root) {
console.log('test');
// if((root.parent.currentItem !== null))
// root.parent.currentItem.current = false;
root.current = !root.current;
root.parent.currentItem = root;
}
}
}
}
Rectangle {
id: container
Layout.fillWidth: true
anchors.top: bar.bottom
implicitHeight: root.height - bar.height
clip: true
Behavior on implicitHeight {
PropertyAnimation { duration: 100 }
}
}
Component.onCompleted: {
if(root.contentItem !== null)
root.contentItem.parent = container;
}
}
}
PanelItem.qml
現在我成功了,我想讓它能夠在ColumnLayout兩個不同PanelItems之間互換位置,同時使用「拖放放下「功能。
在這個時候,我可以拖放我的物品,但我不知道如何交換我的物品之間的位置。
這裏是我的主:
ApplicationWindow {
visible: true
width: 400
height: 400
ColumnLayout {
anchors.fill: parent
spacing: 1
id: test
property var currentItem: null
PanelItem {
title: "Panel 1"
Rectangle {
color: "orange"
anchors.fill: parent
}
}
PanelItem {
title: "Panel 2"
Rectangle {
color: "lightgreen"
anchors.fill: parent
}
}
PanelItem {
title: "Panel 3"
Rectangle {
color: "lightblue"
anchors.fill: parent
}
}
PanelItem {
title: "Panel 4"
Rectangle {
color: "yellow"
anchors.fill: parent
}
}
Item {
Layout.fillWidth: true
Layout.fillHeight: true
}
}
}
main.qml
我看,也許我應該嘗試用一個ListView做,但我不能讓它與我的PanelItems。
你有什麼建議嗎?我有點迷路了QML,Views等。
非常感謝!
你好真皮替換
ColumnLayout
和Repeater
。 感謝您的回答!我選擇按照你所說的使用ListView,但在嘗試更新項目的索引之前,我很難將手風琴放在ListView中。 如果您有時間,請檢查此 [thread](http://stackoverflow.com/questions/43230739/how-to-use-a-listview-with-custom-item-qml)。 祝您有美好的一天! –