Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更改模型后,QTableView中的项目数量保持不变_C++_Qt_View_Model - Fatal编程技术网

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文档明确告诉你的那样,我可能会补充:)