我正在使用ListView组件,我遇到了currentIndex
属性的问题。我想对
onCurrentIndexChanged:
但是当我使用
时LayoutMirroring.enabled: true
currentIndex
不再更新。 onCurrentIndexChanged
中没有任何操作,currentIndex始终等于0。
我也尝试过使用
layoutDirection: Qt.RightToLeft
但问题是相同的(移动listView时没有currentIndex
更新。)
我已经阅读了ListView文档,并且有信息,如果您使用
snapMode: ListView.SnapToItem
你还需要添加
highlightRangeMode: ListView.StrictlyEnforceRange
以便currentIndex正确更新。也许镜子布局有类似的东西?
以下是整个代码:
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 );
}
}
所以我的问题是:在使用currentIndex
或LayoutMirroring.enabled: true
时滚动listView时我需要做些什么来更新layoutDirection: Qt.RightToLeft
。
答案 0 :(得分:0)
好的,我得到了解决方案!
问题在于这条线:
preferredHighlightBegin: id_multiCouplingMeasurePopup.p_bViewRotated ? id_couplingControlView.width : id_couplingControlView.leftMargin
对于旋转视图,我将preferredHighlightBegin
设置为listView本身的宽度。这就是为什么currentIndex没有更新的原因。镜像listView与此无关。
答案 1 :(得分:0)
如果您提供了Minimal, Complete and 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)
}
}