C++ 更改模型后,QTableView中的项目数量保持不变
我有一个具有以下模型的虚拟表视图实现:C++ 更改模型后,QTableView中的项目数量保持不变,c++,qt,view,model,C++,Qt,View,Model,我有一个具有以下模型的虚拟表视图实现: class MyModel: public QAbstractListModel { int columnCount (const QModelIndex & parent = QModelIndex() ) const { return 2; } int rowCount (const QModelIndex & parent = QModelIndex() ) const { return count; } Q
class MyModel: public QAbstractListModel
{
int columnCount (const QModelIndex & parent = QModelIndex() ) const { return 2; }
int rowCount (const QModelIndex & parent = QModelIndex() ) const { return count; }
QModelIndex parent (const QModelIndex & index ) const { return QModelIndex(); }
QModelIndex index (int row, int column, const QModelIndex & parent = QModelIndex() ) const { return createIndex(row, column); }
QVariant data(const QModelIndex & index, int role) const
{
int col = index.column();
int row = index.row();
if (role == Qt::DecorationRole && col == 0)
{
return getIcon(row); // icons in the first column
}
else if (role == Qt::DisplayRole && col == 1)
{
return getText(row); // text in the second column
}
else
{
return QVariant();
}
}
void update()
{
getNewText();
getNewIcons();
emit dataChanged((index(0,0)), index(count-1,1));
}
}
在创建表视图并第一次分配模型之后,一切正常:比如说,我在表视图中得到了10个项目
但是后来我更新了模型,现在它有12项。仅显示其中的前10个。看起来它缓存了10的值,不想更新它
如何解决这个问题?我通过调用
更新方法中的开始删除
,结束删除
,开始插入行
,结束插入行
解决了这个问题,就像Qt文档明确告诉你的那样,我可能会补充:)