gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:02 29 4
gpt4 key购买 nike

我已经基于类似于此的 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com