【问题标题】:QML PathView: change current index of pathview via mouse wheelQML PathView:通过鼠标滚轮更改路径视图的当前索引
【发布时间】:2014-03-09 09:36:30
【问题描述】:

我创建了一个带有委托和模型的路径视图。是否可以通过鼠标滚轮而不是鼠标单击拖动来更改 PathView 的当前索引?

以下是我的 PathView 代码:

PathView {
    id: pathView;
    anchors.fill: parent;
    model: sampleModel;
    delegate: delegate;
    path: Path {
        startX: 45; startY: 100
        PathAttribute { name: "iconScale"; value: 0.3 }
        PathAttribute { name: "iconOpacity"; value: 0.1 }
        PathQuad { x: 45; y: 300; controlX: 45; controlY: 200 }
        PathAttribute { name: "iconScale"; value: 1 }
        PathAttribute { name: "iconOpacity"; value: 1 }
        PathQuad { x: 45; y: 500; controlX: 45; controlY: 400 }
    }
}

【问题讨论】:

    标签: qt qml


    【解决方案1】:

    我遇到了同样的问题。这是我的解决方案: 每次发生车轮事件时,我都会输入/减少“wheelIndex”并将 view.currentIndex 设置为该值。 为了在轮子使用后重置轮子索引,我使用了一个计时器:

    PathView {
    id: view
    anchors.fill: parent
    focus: true
    model: monthModel
    property int wheelIndex:- 1;
    currentIndex: 0;
    
    MouseArea {
    anchors.fill: parent
    preventStealing: true;
    propagateComposedEvents: true
    
     Timer {
         id:wheelTimer
         interval: 200;
         running: false;
         repeat: false
         onTriggered: {
            view.wheelIndex = -1;
         }
     }
    
    onWheel: {
        wheelTimer.start();
    
        if (view.wheelIndex === -1) {
            view.wheelIndex = view.currentIndex;
        }
    
        if (wheel.angleDelta.y > 0) {
            view.wheelIndex++
            if (view.wheelIndex > view.model.count) {
                view.wheelIndex = 0;
            }
    
            view.currentIndex = view.wheelIndex;
        } else {
            view.wheelIndex--;
            if (view.wheelIndex < 0) {
                view.wheelIndex = view.model.count - 1;
            }
    
            view.currentIndex = view.wheelIndex;
        }
    }
    }
    

    }

    【讨论】:

      【解决方案2】:

      通过使用 anchors.fill:parentMouseArea 作为 child 添加到您的 PathView 并实现 onWheel() 事件如下:

       PathView{
               id:view
               ...... 
      
          MouseArea{
      
             id:mouse
             anchors.fill: parent
             onWheel: 
                  {
                 if( wheel.angleDelta.y > 0 ) view.incrementCurrentIndex();
                 else view.decrementCurrentIndex();
                  }
          }
      

      您可以使用鼠标拖动鼠标滚动(滚轮)。更多信息请查看QtQuick 5.0: WheelEvent

      【讨论】:

      • 谢谢,但是这种方式有一个问题:用户滚轮滚动的速度不会影响pathView的速度。原因是路径视图的动画行为。
      • 我对 PathView 进行了一些调查,并在 Wheel 事件 中通过 PathView.movingPathView.dgraggingPathView.flicking 注意到(错误所有这些)PathView.incrementIndex()PathView.decrementIndex() 不会发出 flicking 事件。因此我们没有得到您想要的行为和漂亮的动画。
      • 我想通过将PathView.flicking 设置为true 来欺骗它,但它是只读的并且没有可用的设置器。希望这会启发你的道路。
      猜你喜欢
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      相关资源
      最近更新 更多