2015-12-08 91 views
0

我已經使用model-view-delegate範例實現了一個簡單的QML應用程序。在我的申請中,我使用highlight屬性來強調我的ListView當前選定的項目。選擇工作正常,但是當我點擊遠處的項目時,突出顯示的移動速度非常緩慢。Listview突出顯示移動非常緩慢

請看下面的例子:

import QtQuick 2.5 

ApplicationWindow { 
    width: 500 
    height: 400 
    title: qsTr("List test") 
    visible: true 

    ListView { 
     id: view 
     anchors.fill: parent 
     model: 20 

     delegate: Rectangle { 
      border.color: "steelblue" 
      color: Qt.lighter(border.color) 
      width: ListView.view.width 
      height: 20 

      Text { anchors.centerIn: parent; z: 2; text: index + 1 } 

      MouseArea { 
       anchors.fill: parent 
       onClicked: view.currentIndex = index 
      } 
     } 

     highlight: Rectangle { 
      border.color: "yellow" 
      border.width: 3 
      color: "transparent" 
      height: 20 
      width: ListView.view.width 
      z: Infinity 
     } 
    } 
} 

如果你選擇的最後一個元素,突出顯示了移動所有其他物品抵達選擇一個之前。這不是我期望的行爲。我如何才能將突出顯示直接移到最後?

回答

3

你經歷了它的預期一個行爲,根據highlightFollowsCurrentItem文件:

這個屬性保存的亮點是通過視圖管理。

如果此屬性爲true(默認值),則高亮將平滑移動以跟蹤當前項目。否則,突出顯示不會被視圖移動,並且必須通過突出顯示來執行任何移動。

亮點動畫是通過highlightMoveDurationhighlightMoveVelocity屬性控制。速度設置爲400 pixels/second,該值可以對應於長視野高密度設備上的長時間運行的動畫。

你能解決這個問題有兩種不同的方式:通過微調上述特性(例如通過設置一個很小的highlightMoveDuration或高highlightMoveVelocity

  • 通過設置highlightFollowsCurrentItemfalse直接

    • 管理突出顯示的動畫

    在第二種情況下,您放棄動畫並直接綁定突出顯示y位置wi當前所選代表的第y。這樣,突出顯示即時移動到所選代表,如下例所示:

    import QtQuick 2.5 
    
    ApplicationWindow { 
        width: 500 
        height: 400 
        title: qsTr("List test") 
        visible: true 
    
        ListView { 
         id: view 
         anchors.fill: parent 
         model: 20 
    
         highlightFollowsCurrentItem: false // force discarding default animation 
    
         delegate: Rectangle { 
          border.color: "steelblue" 
          color: Qt.lighter(border.color) 
          width: ListView.view.width 
          height: 20 
    
          Text { anchors.centerIn: parent; z: 2; text: index + 1 } 
    
          MouseArea { 
           anchors.fill: parent 
           onClicked: view.currentIndex = index 
          } 
         } 
    
         highlight: Rectangle { 
          border.color: "yellow" 
          border.width: 3 
          color: "transparent" 
          height: 20 
          width: ListView.view.width 
          y: view.currentItem.y  // highlighting direct binding to selected item! 
          z: Infinity 
         } 
        } 
    }