【问题标题】:Hide the highlight of a ListView while scrolling滚动时隐藏 ListView 的突出显示
【发布时间】:2013-06-15 20:35:22
【问题描述】:

我正在为 UI 构建一个基于 Qt-Quick 2 的 Qt5 应用程序。显示带有高亮组件的 ListView 时遇到问题。当我滚动 ListView 时,突出显示矩形在 ListView 之外可见,我找不到避免它的方法。

以下是最小 QML 文件的问题示例:

import QtQuick 2.0

Rectangle {
    width: 360; height: 600

  ListView {
    width: 350; height: 200
    anchors.centerIn: parent
    id: myList
    model: myModel
    highlight: highlightBar

    delegate: Item {
      width: 400; height: 20
      Text { text: name }

      MouseArea {
        id: mArea
        anchors.fill: parent
        onClicked: { myList.currentIndex = index; }
      }
    }
  }

  Component {
    id: highlightBar
    Rectangle {
      width: parent.width; height: 20
      color: "#FFFF88"
    }
  }

  ListModel {
      id: myModel
  }

  /* Fill the model with default values on startup */
  Component.onCompleted: {
      for(var i = 0; i < 100; i++) {
          myModel.append({ name: "Big Animal : " + i});
      }
  }
}

有没有办法将组件“限制”到其父边框或在滚动时隐藏高亮组件?

【问题讨论】:

    标签: qml qt5 qtquick2


    【解决方案1】:

    documentation报道:

    注意:视图不会自动启用剪辑。如果视图没有被其他项目或屏幕剪辑,则需要设置 clip: true 以便很好地剪辑视图外的项目。

    因此,您遇到的是一种常见行为,您应该 1) 通过其他 Items 剪辑视图(例如,标题 Rectangle 和页脚 Rectanglez:infinite 或简单地设置 @ 987654327@属性转true,即

    ListView{
       //...
       clip:true
       //...
    }
    

    Clipping 有一些perfomance disavantages,随着应用程序的增长,它们会极大地影响应用程序。因此,应仔细评估其使用情况,尤其是在视图场景之外。

    【讨论】:

    • 现在看起来很明显,但我花了一天时间尝试了尽可能多的属性,但完全错过了这个,非常感谢!!
    • @koopajah 发生在我们当中:) 只是一个建议:当您无法找到特定属性时,请尝试查看 qml 的 item 元素。很可能你会找到它。
    • 该属性默认为true。我因此浪费了 2 小时 :(
    猜你喜欢
    • 1970-01-01
    • 2015-02-15
    • 2013-04-24
    • 1970-01-01
    • 2014-05-26
    • 2020-02-03
    • 1970-01-01
    • 2014-10-15
    • 2012-10-26
    相关资源
    最近更新 更多