【问题标题】:Stop the Pathview movement at the last element在最后一个元素处停止 Pathview 移动
【发布时间】:2017-02-19 12:43:42
【问题描述】:

如何在 QML 中的 Pathview 元素到达模型中的最后一个元素时停止移动(如列表视图和网格视图默认行为)。即,我不想要循环移动。

在下面的代码中,如果我们在第一个(“第一个”)项目后向右滑动,最后一个(“最后一个”)将显示(然后是“Jane Doe”)。可以做些什么来阻止第一次向右进一步滑动到达并在到达最后一项时进一步向左滑动。到达第一项或最后一项时应停止

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ListModel {
        id:contactModel
         ListElement {
             name: "first"
             icon: "pic.png"
         }
         ListElement {
             name: "Jane Doe"
             icon: "pic.png"
         }
         ListElement {
             name: "John Smith"
             icon: "pic.png"
         }

         ListElement {
             name: "Bill Jones"
             icon: "pic.png"
         }
         ListElement {
             name: "Jane Doe"
             icon: "pic.png"
         }
         ListElement {
             name: "John Smith"
             icon: "pic.png"
         }
         ListElement {
             name: "Bill Jones"
             icon: "pic.png"
         }
         ListElement {
             name: "Jane Doe"
             icon: "pic.png"
         }
         ListElement {
             name: "last"
             icon: "pic.png"
         }
     }

         Component {
             id: delegate
             Column {
                 id: wrapper
                 Image {
                     anchors.horizontalCenter: nameText.horizontalCenter
                     width: 64; height: 64
                     source: icon
                 }
                 Text {
                     id: nameText
                     text: name
                     font.pointSize: 5
                     color: wrapper.PathView.isCurrentItem ? "red" : "black"
                 }
             }
         }

         PathView {
             anchors.fill: parent
             model: contactModel
             delegate: delegate
             snapMode:PathView.SnapOneItem
             path: Path {
                 startX: 0; startY: 100


                 PathLine { x: 640; y: 400;  }
             }
         }

}

【问题讨论】:

  • 请包含您目前编写的代码。您提供的详细信息越多,您可能收到的答案就越多。检查FAQHow to ask

标签: qt qml qt-creator


【解决方案1】:

这是实现的样子(我无法找到可用的属性来做到这一点)

PathView {
    property int previousCurrentIndex: 0
    currentIndex: 0
    ...

    onCurrentIndexChanged: {
        var lastIndex = contactModel.count - 1
        if (currentIndex == 0 && previousCurrentIndex == lastIndex) {
            pathViewId.positionViewAtIndex(lastIndex, PathView.Beginning)
        } else if (currentIndex == lastIndex && previousCurrentIndex == 0) {
            pathViewId.positionViewAtIndex(0, PathView.Beginning)
        }
        previousCurrentIndex = currentIndex
    }
}

引入自定义属性previousCurrentIndex,并在发出onCurrentIndexChanged信号时计算实际currentIndex

【讨论】:

  • 对不起,这不是预期的。我已经编辑了问题。我想停止路径视图的进一步移动
  • 请检查已编辑的答案,它限制了 PathView 在数据模型中具有最后一个索引的项目上的移动。您应该能够根据您的需要调整此解决方案。
【解决方案2】:

您可以在 PathView 中添加以下代码。它会阻止视图从结尾移动到开头,反之亦然。

property real previousOffsetVal: 0.

onOffsetChanged: {
            /* 10 is a threshhold, should be smaller than the total elements count*/
            if (Math.abs( previousOffsetVal - offset) > 10 ) {
                offset = previousOffsetVal
            }

            previousOffsetVal = offset
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多