简体   繁体   English

QML ListView计数为零,只有一些行可见

[英]QML ListView count is zero and only some rows are visible

I have created a ListView model based on QAbstractListModel similar to this example . 我已经基于QAbstractListModel创建了一个与本示例类似的ListView模型。 The problem is my ListView doesn't show all rows, but only those which are initialy visible. 问题是我的ListView不会显示所有行,而只会显示最初可见的那些行。 So when I scroll (or flick) down theres only black space. 因此,当我向下滚动(或滑动)时,只有黑色空间。 ListView has count == 0 but it should have count == 10, since I've added 10 elements. ListView的count == 0,但是它应该有count == 10,因为我已经添加了10个元素。

My class contains the necessary mothods to update the model rowCount 我的课程包含了更新模型rowCount的必要方法

// needed as implementation of virtual method
int rowCount(const QModelIndex & parent) const {
    return standings.size();
}
int rowCount() {
    return standings.size();
}

void addStanding(const Standing &st) {
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    qDebug() << "row cnt: " << rowCount();
    standings << st;
    endInsertRows();
}

I also update the ListView model 我还更新了ListView模型

rootContext = (QDeclarativeContext*)(viewer->rootContext());
rootContext->setContextProperty("myModel", &sm);

QML code QML代码

ListView {
            id: raceList
            objectName: "standingList"
            y: header.height
            width: parent.width
            height: parent.height - header.height
            clip: true
            model: myModel
            delegate: rowComp
        }
        Component {
            id: rowComp
            SessionRowDelegate { }
        }

I would also like to mention that this works fine with a static model. 我还要提及的是,此模型在静态模型下可以正常工作。 How to make ListView display all items all the time and not only if visible to user? 如何使ListView始终显示所有项目,而不仅是对用户可见?

SessionRowDelegate SessionRowDelegate

Rectangle {
  id: rowbg
  width: parent.width
  height: 34 * (1 + (parent.width - 360) * 0.002)

  // contains other elements with height not exceeding rowbg's height
  // ...
}

It is a bit late (4 years!), but there is something related to this problem that I have discovered after days of getting stuck! 有点晚了(4年!),但是在卡住了几天后,我发现了一些与此问题有关的东西! The problem is mainly based in the weird way ListView deals with the models. 问题主要是基于ListView处理模型的怪异方式。

After overriding the rowCount method in my own model, I faced the exact problem that you have mentioned. 在我自己的模型中重写rowCount方法后,我遇到了您提到的确切问题。 The funny thing is, they are NEVER called. 有趣的是,它们从未被调用过。 ListView actually never calls to get the rowCount, instead queries for the amount in cacheBuffer size (which defaults to 512 on my system), that is why it won't notice if the items are more than the cacheBuffer size or the default it is using, it only notices that when it is scrolled to the end. ListView实际上从不调用获取rowCount,而是查询cacheBuffer大小(在我的系统上默认为512),这就是为什么它不会注意到项目是否大于cacheBuffer大小或它使用的默认值的原因,它只会在滚动到末尾时才注意到。


While this will shed light on some other people problems, it is not the solution for your problem. 尽管这将为其他一些人的问题提供启发,但它并不是解决您的问题的方法。 In your case, if the items are not that many, I suggest tweaking the ListView cacheBuffer size, and increasing it as much as you need. 在您的情况下,如果项不是很多,我建议调整ListView cacheBuffer的大小,并根据需要增加它的大小。 Just keep in mind that if your elements get numerous, it can have a huge performance penalty. 请记住,如果您的元素数量众多,则可能会导致巨大的性能损失。

A ListView only renders the elements that are currently visible plus a few that are pre rendered to be shown when scrolling. ListView仅呈现当前可见的元素以及滚动显示时预先显示的元素。 The default implementation does not render all of them for performance reasons. 由于性能原因,默认实现不会呈现所有视图。 Any references to the size of the list should refer to the model object instead of the ListView itself. 对列表大小的任何引用都应引用模型对象,而不是ListView本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM