この例のような QAbstractListModel に基づいて ListView モデルを作成しました。問題は、私の ListView がすべての行を表示するのではなく、最初に表示されている行のみを表示することです。そのため、下にスクロール (またはフリック) すると、黒いスペースしかありません。ListView には count == 0 がありますが、10 個の要素を追加したので、count == 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にすべてのアイテムを常に表示させる方法は?
セッション行デリゲート
Rectangle {
id: rowbg
width: parent.width
height: 34 * (1 + (parent.width - 360) * 0.002)
// contains other elements with height not exceeding rowbg's height
// ...
}