【发布时间】:2017-04-05 12:07:03
【问题描述】:
我是 QML 的新手。我感谢互联网资源这手风琴:
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: {
root.Drag.drop()
}
onClicked: {
if (root.parent.current !== root) {
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
Window {
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 中,但我迷路了,它根本不起作用。
我尝试过类似的方法:
Window {
visible: true
width: 400; height: 400
ListView {
id: my_list
anchors.fill: parent
model: 14
delegate: PanelItem {
id: my_delegate
title: "Panel 1"
Rectangle {
color: "orange"
anchors.fill: parent
}
}
}
}
main.qml
有人可以帮我做并解释我做错了什么吗?
非常感谢!
【问题讨论】:
-
A
ListView不是Layout,因此您在委托中使用的附加属性Layout(例如Layout.fillWidth: true)不应该可用。尝试用锚点来代替父项的左侧和右侧。