我創建了一個基於QAbstractListModel的ListView模型,類似於此。 問題是我的ListView不顯示所有行,但只有那些初始化可見。所以當我滾動(或輕彈)只有黑色空間。 ListView的計數== 0,但它應該有count == 10,因爲我已經添加了10個元素。QML ListView計數爲零,只有一些行可見
我的類包含必要的mothods更新模型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始終顯示所有項目,而不僅僅在用戶可見時才顯示所有項目?
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
// ...
}
您可以發表QML代碼嗎? –
如何使SessionRowDelegate看起來像?最重要的是,列表視圖的所有行都是統一的高度?如果不是,ListView將無法正常工作。 – sebasgo
是的,SessionRowDelegate的高度已設置。列表視圖的所有行都具有統一的高度。 – dbow