我正在使用ListView组件,currentIndex
属性有问题。我想做一些操作
onCurrentIndexChanged:
但是当我使用
LayoutMirroring.enabled: true
currentIndex
不再更新。onCurrentIndexChanged
中没有操作,当前索引始终等于 0。我也尝试使用
layoutDirection: Qt.RightToLeft
但问题是相同的(移动列表视图时没有currentIndex
更新(。
我已经阅读了ListView文档,并且有信息,如果您使用
snapMode: ListView.SnapToItem
您还需要添加
highlightRangeMode: ListView.StrictlyEnforceRange
为了当前索引正确更新。也许镜子布局也有类似的东西?
这是整个代码:
ListView
{
id: id_couplingControlView
model: id_multiCouplingMeasurePopup.p_iCouplingsCount
orientation: ListView.Horizontal
snapMode: ListView.SnapToItem
highlightRangeMode: ListView.StrictlyEnforceRange //To update the currentIndex as the list is moved
preferredHighlightBegin: id_multiCouplingMeasurePopup.p_bViewRotated ? id_couplingControlView.width : id_couplingControlView.leftMargin
LayoutMirroring.enabled: id_multiCouplingMeasurePopup.p_bViewRotated
//layoutDirection: id_multiCouplingMeasurePopup.p_bViewRotated ? Qt.RightToLeft : Qt.LeftToRight
delegate: QmlSHMultiCouplingMeasureControl
{
id: id_CouplingMeasureControl
p_iIndex: index
Component.onCompleted:
{
InitializeCouplingControl ( id_CouplingMeasureControl )
}
}
//Need to to something here
onCurrentIndexChanged:
{
console.log ( "onCurrentIndexChanged:: " + id_couplingControlView.currentIndex )
id_multiCouplingMeasurePopup.UpdateMiniTrainCouplingList( id_couplingControlView.currentIndex );
}
}
所以我的问题是:在使用 LayoutMirroring.enabled: true
或 layoutDirection: Qt.RightToLeft
时滚动列表视图时,我需要做什么才能更新currentIndex
。
我得到了解决方案!
问题是因为这一行:
preferredHighlightBegin: id_multiCouplingMeasurePopup.p_bViewRotated ? id_couplingControlView.width : id_couplingControlView.leftMargin
对于旋转视图,我preferredHighlightBegin
设置与列表视图本身的宽度一样宽。这就是当前索引没有更新的原因。镜像列表视图与此无关。
如果您提供了 Minimal、Complete 和 Verifyable Example,我们可能会归结为问题的核心。
所以要回答你的问题:你需要做的,正是你假设的:
- 使
ListView
水平:orientation: ListView.Horizontal
- 启用镜像:
LayoutMirroring.enabled: true
- 对齐项目:
snapMode: ListView.SnapToItem
<= 实际上似乎是可选的 - 强制
currenItem
在视图中:highlightRangeMode: ListView.StrictlyEnforceRange
这就是MCVE的样子,证明它按预期工作:
ListView {
id: lv
model: 100
LayoutMirroring.enabled: true
orientation: ListView.Horizontal
width: 500
height: 100
spacing: 5
snapMode: ListView.SnapToItem
highlightRangeMode: ListView.StrictlyEnforceRange
onCurrentIndexChanged: console.log('Current Index changed to', currentIndex)
delegate: Rectangle {
width: 100
height: 100
color: lv.currentItem === this ? Qt.rgba(1, 0, 0, 1) : Qt.rgba(0, 0, 1, index / 200 + 0.5)
}
}