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

标签 c++ qt listview symbian qml

我已经基于类似于此的 QAbstractListModel 创建了一个 ListView 模型 example . 问题是我的 ListView 不显示所有行,而只显示最初可见的行。所以当我向下滚动(或轻弹)时,只有黑色空间。 ListView 有 count == 0 但它应该有 count == 10,因为我已经添加了 10 个元素。

我的类包含更新模型 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();
}

我还更新了 ListView 模型

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

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 { }
        }

我还想提一下,这适用于静态模型。 如何让 ListView 始终显示所有项目,而不仅仅是在对用户可见时显示?

session 行委托(delegate)

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

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

最佳答案

有点晚了(4 年!),但在卡住了几天后我发现了与这个问题相关的东西!问题主要在于 ListView 处理模型的奇怪方式。

在我自己的模型中覆盖 rowCount 方法后,我遇到了您提到的确切问题。有趣的是,他们从来没有被调用过。 ListView 实际上从不调用获取 rowCount,而是查询 cacheBuffer 大小的数量(在我的系统上默认为 512),这就是为什么它不会注意到项目是否超过 cacheBuffer 大小或它使用的默认值,它只会在滚动到末尾时注意到。


虽然这会阐明一些其他人的问题,但这不是您问题的解决方案。在你的情况下,如果项目不是那么多,我建议调整 ListView cacheBuffer 的大小,并根据需要增加它。请记住,如果您的元素过多,可能会导致巨大的性能损失。

关于c++ - QML ListView 计数为零,只有一些行可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748161/

相关文章:

c++ - 如果我将 double 转换为 int,但 double 的值超出范围会怎样?

java - 如何使用 Java Android SDK 做好实时数据流

android - 获取一个ListView刷新? YouTube API-自定义适配器

ios - 我怎样才能得到像下面的 Apple Music 播放列表概述这样的 ListView ?

c++ - 从 LPWSTR 转换为 LPTSTR

c++ - 2s补码是一种存储负数的方法吗?

c++ - 编译器代码生成错误或内存损坏?

c++ - 使用 QXmlStreamReader C/Qt 解析 XML 文件

c++ - Qt:如何从 C++ 检查对象是否为 QML 类型 Foo

c# - 使用 C# 和 XAML 在 Windows Phone 8.1 应用程序中更新或刷新 Listview