当列表视图的委托超出范围时,Qml Combobox 不关闭
Qml Combobox not closing when delegate of the listview goes out of scope
我为包含 Combobox 的列表视图创建了一个委托。如果我打开 Combobox 并滚动列表视图,Combobox 弹出窗口会随着代理位置移动,没关系。但是当代表离开列表视图区域时(参考附加的示例图像),Combobox 弹出窗口甚至继续移出列表视图区域。
如何在相应委托离开列表视图区域时关闭组合框。
提前致谢...
代码在这里
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Column {
spacing: 0
anchors.fill: parent
Item {
width: parent.width
height: parent.height * 0.4
Image {
anchors.fill: parent
anchors.margins: 10
source: "https://lh4.googleusercontent.com/proxy/cITVCAj9KJ5Hfwd5iuNDhzdB2pSrMQv2rzTl-vvg23Ifhe2qdCisZBG-MzV35y_r2zijC9X4QOpda9eHzr_hA"
}
}
ListView {
width: parent.width
height: parent.height * 0.7
model: 10
spacing: 5
clip: true
delegate: Rectangle {
width: parent.width
height: 50
color: index % 2 == 0 ? "lightsteelblue" : "steelblue"
Row {
spacing: 25
anchors.centerIn: parent
Label {
text: qsTr("%1").arg(index)
anchors.verticalCenter: parent.verticalCenter
}
ComboBox {
anchors.verticalCenter: parent.verticalCenter
model: ["a", "b", "c"]
}
}
}
}
}
}
如果没有特定目标要在滚动时保持 ComboBox
弹出窗口打开,则将以下 属性 添加到您的 ListView
:
highlightRangeMode: ListView.StrictlyEnforceRange
这将在滚动 ListView
时关闭 ComboBox
弹出窗口。
P.S.
此外,还解决了 ComboBox
列表超出视野范围的问题。
关于 header
元素隐藏在其他列表项下方的问题的更新:
根据描述 ListView.StrictlyEnforceRange - the highlight never moves outside of the range. The current item changes if a keyboard or mouse action would cause the highlight to move outside of the range.
当一个项目超出范围时,列表会更改下一个项目,这使得 ComboBox
关闭其弹出窗口,但由于 header 项目在另一个项目下方ListView
项目本身(参见本段 https://doc.qt.io/qt-5/qml-qtquick-listview.html#stacking-order-in-listview ,代表总是在 header 之上)它不可能在其他项目的顶部显示默认 header 。我建议您在列表之外实现自己的 header。抱歉,我可能不太了解 Qt 以找到其他解决方案。
我为包含 Combobox 的列表视图创建了一个委托。如果我打开 Combobox 并滚动列表视图,Combobox 弹出窗口会随着代理位置移动,没关系。但是当代表离开列表视图区域时(参考附加的示例图像),Combobox 弹出窗口甚至继续移出列表视图区域。
如何在相应委托离开列表视图区域时关闭组合框。
提前致谢...
代码在这里
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Column {
spacing: 0
anchors.fill: parent
Item {
width: parent.width
height: parent.height * 0.4
Image {
anchors.fill: parent
anchors.margins: 10
source: "https://lh4.googleusercontent.com/proxy/cITVCAj9KJ5Hfwd5iuNDhzdB2pSrMQv2rzTl-vvg23Ifhe2qdCisZBG-MzV35y_r2zijC9X4QOpda9eHzr_hA"
}
}
ListView {
width: parent.width
height: parent.height * 0.7
model: 10
spacing: 5
clip: true
delegate: Rectangle {
width: parent.width
height: 50
color: index % 2 == 0 ? "lightsteelblue" : "steelblue"
Row {
spacing: 25
anchors.centerIn: parent
Label {
text: qsTr("%1").arg(index)
anchors.verticalCenter: parent.verticalCenter
}
ComboBox {
anchors.verticalCenter: parent.verticalCenter
model: ["a", "b", "c"]
}
}
}
}
}
}
如果没有特定目标要在滚动时保持 ComboBox
弹出窗口打开,则将以下 属性 添加到您的 ListView
:
highlightRangeMode: ListView.StrictlyEnforceRange
这将在滚动 ListView
时关闭 ComboBox
弹出窗口。
P.S.
此外,还解决了 ComboBox
列表超出视野范围的问题。
关于 header
元素隐藏在其他列表项下方的问题的更新:
根据描述 ListView.StrictlyEnforceRange - the highlight never moves outside of the range. The current item changes if a keyboard or mouse action would cause the highlight to move outside of the range.
当一个项目超出范围时,列表会更改下一个项目,这使得 ComboBox
关闭其弹出窗口,但由于 header 项目在另一个项目下方ListView
项目本身(参见本段 https://doc.qt.io/qt-5/qml-qtquick-listview.html#stacking-order-in-listview ,代表总是在 header 之上)它不可能在其他项目的顶部显示默认 header 。我建议您在列表之外实现自己的 header。抱歉,我可能不太了解 Qt 以找到其他解决方案。