滚动时隐藏 ListView 的突出显示 [英] Hide the highlight of a ListView while scrolling

查看:33
本文介绍了滚动时隐藏 ListView 的突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a Qt5 application based on Qt-Quick 2 for the UI. I have an issue while displaying a ListView with an highlight component. When I scroll the ListView the highlight rectangle is visible outside of the ListView and I can't find a way to avoid it.

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

Here is an example of the issue with a minimal QML file:

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});
      }
  }
}

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

Is there a way to "limit" a component to its parent borders or to hide the highlight component while scrolling?

推荐答案

文档:

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

Note: Views do not enable clip automatically. If the view is not clipped by another item or the screen, it will be necessary to set clip: true in order to have the out of view items clipped nicely.

因此,您遇到的是一种常见行为,您应该 1) 通过其他 Item(例如标题 Rectangle 和页脚 >Rectanglez:infinite 或简单地将 clip 属性设置为 true,即

Hence, what you are experiencing is a common behaviour and you should either 1) clip the view via other Items (e.g. a header Rectangle and a footer Rectangle with z:infinite or simply set the clip property to true, i.e.

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

剪辑有一些性能劣势,这会极大地影响随着应用程序的增长.因此,应该仔细评估它的使用,尤其是在视图场景之外.

Clipping has some perfomance disavantages which can greatly affect the application as it grows. Hence, its usage, especially outside the views scenario, should be evaluated carefully.

这篇关于滚动时隐藏 ListView 的突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆