代码之家  ›  专栏  ›  技术社区  ›  Ivan Fateev

QML列表视图间距故障

  •  0
  • Ivan Fateev  · 技术社区  · 9 年前

    import QtQuick 2.5
    import QtQuick.Controls 2.0
    
    Page {
    
        ListView {
            anchors.fill: parent
            model: 50
            delegate: Item {
                clip: true
                height: 50
                anchors {
                    left: parent.left
                    right: parent.right
                }
    
                Rectangle {
                    color: "red"
                    anchors.fill: parent
                }
                Label {
                    text: index
                }
    
            }
        }
    
    }
    

    有时,当我滚动时,我看到行之间的间距,但间距为零。我想这是某种坐标舍入误差。我找到了问题的可能来源——剪辑:委托内部的true。如果我把它去掉,一切都好。

    这是Qt的一个bug,我如何解决它?

    enter image description here

    2 回复  |  直到 9 年前
        1
  •  1
  •   derM - not here for BOT dreams    9 年前

    如果你只有那些 故障 启用剪裁标签时,不要使用它-至少不要在代理的根节点中使用。

    Page {
        ListView {
            anchors.fill: parent
            model: 50
            delegate: Rectangle {
                clip: false // <-- Do not clip in the delegate's root node
                height: 50
                color: "red"
                anchors {
                    left: parent.left
                    right: parent.right
                }
                Item {
                    anchors.fill: parent
                    clip: true // <-- instead you might clip in a delegate's child node
                    Label {
                        text: index + 'a very long string that might be clipped at some point'
                    }
                }
            }
        }  
    }
    

    至少在我的电脑上没有这些小故障。 但是,我会尽量不剪裁代理,因为剪裁是一个性能因素,可能会产生影响,尤其是当对象移动时(例如,在 ListView )

        2
  •  1
  •   Ivan Fateev    9 年前

    ListView的pixelAligned属性绝对解决了我的问题

    ListView {
        ...
        pixelAligned: true
        ...
    }