为什么ListView中的转发器需要剪辑?

时间:2016-05-30 17:56:17

标签: qt qml qt5 qt5.6

我遇到了这个问题,其中ListView的行并不总是正确排列。看起来它可能是一个bug,但我有一个解决方法,修剪它。但这只是一次意外吗?

问题与模型中有大量行有关,但我的所有行都是整数高度并且彼此相同,所以这不应该是一个问题。

main.qml

import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.2

ApplicationWindow
{
    visible: true
    width: 640
    height: 800

    property int rowHeaderHeight: 24

    // make a list of rows each with the model row height + header height
    ListView
    {
        id: boxview
        anchors.fill: parent

        // calculate content height (line can be omitted without problem)
        contentHeight: (myModel.rowHeight()+rowHeaderHeight)*myModel.rowCount()
        model: myModel

        delegate: Thing1 { name: model.name; headerHeight: rowHeaderHeight }
        Component.onCompleted:
        {
            // go right to the middle
            boxview.positionViewAtIndex(500000, ListView.Beginning);
        }
    }
}

Thing.qml

import QtQuick 2.5
import QtQuick.Controls 1.4

Item
{
    // thing is just a box with a header label

    width: parent.width
    height: myModel.rowHeight() + headerHeight

    property string name
    property int headerHeight

    // label is the header height
    Label
    {
        width: parent.width
        height: headerHeight
        verticalAlignment: Text.AlignBottom
        text: name
        x: 8
    }

    // then the box
    Repeater
    {
        // but wait! there are 10 of them in the same place
        // yes, it works for 1, but goes wrong the more there are
        // 10 makes it look obviousl
        model: 10
        delegate: Rectangle
        {
            //clip: true   // uncomment this to fix the problem, but why?
            y: headerHeight
            width: parent.width;
            height: myModel.rowHeight()
            color: "blue"
        }
    }
}

mymodel.h

#include <QAbstractListModel>
#include <QQmlApplicationEngine>
#include <QObject>
#include <QString>
#include <QDebug>

class MyModel : public QAbstractListModel
{
    Q_OBJECT

public:

    int _rowCount;
    int _rowHeight;

    MyModel()
    {
        _rowCount = 1000000;
        _rowHeight = 100;
    }

    int rowCount(const QModelIndex& parent = QModelIndex()) const override
    {
        return _rowCount;
    }  

    QVariant data(const QModelIndex &index, int role) const override
    {
        int ix = index.row();
        char buf[128];
        sprintf(buf, "Box %d", ix);
        return buf;
    }

    QHash<int, QByteArray> roleNames() const override
    {
        QHash<int, QByteArray> roles;
        roles[Qt::UserRole+1] = "name";
        return roles;
    }

    Q_INVOKABLE int rowHeight()
    {
        return _rowHeight;
    }
};

的main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qqmlcontext.h>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include "mymodel.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    MyModel model;

    QQmlContext* ctxt = engine.rootContext();
    ctxt->setContextProperty("myModel",  &model);
    engine.addImportPath(":/.");
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

这是你向下滚动时所看到的内容。注意标题与蓝色框重叠。

enter image description here

为什么会这样?

如果repeater更改为1而不是10,它会消失,但我认为这样做的可能性较小。

如果clip:true行已注释,则可行,但我不知道原因。

这里是项目文件的要点。 https://gist.github.com/anonymous/8c314426fe4f8764e22819f63e7f50fc

qt5.6 /窗/ mingw的

感谢任何信息。

1 个答案:

答案 0 :(得分:1)

看起来像QTBUG-43193。从快速查看,ListView将可见项目的索引乘以委托高度,因此可能存在大型模型计数的精度问题。