C++ QT-QAbstractTableModel实现insertRows

C++ QT-QAbstractTableModel实现insertRows,c++,qt,qabstracttablemodel,qsortfilterproxymodel,C++,Qt,Qabstracttablemodel,Qsortfilterproxymodel,我有一个QAbstractTableModel的Impl,它有一个向量项,表中的所有项(每行一项) 现在我尝试实现一个添加行(项目I)方法: void add_row(Item i){ int row = rowCount(QModelIndex())+1; beginInsertRows(QModelIndex(), row, row); items.push_back(i); endInsertRows(); emit dataChanged(QModel

我有一个QAbstractTableModel的Impl,它有一个
向量项
,表中的所有项(每行一项)

现在我尝试实现一个
添加行(项目I)
方法:

void add_row(Item i){

   int row = rowCount(QModelIndex())+1;
   beginInsertRows(QModelIndex(), row, row);

   items.push_back(i);

   endInsertRows();
   emit dataChanged(QModelIndex(), QModelIndex());
}
QAbstractTableModel在我的小部件中实例化:

MyQSortFilterProxyModel my_filter = new MyQSortFilterProxyModel();
MyTableModel* my_model = new MyTableModel(items, this);
my_filter->setSourceModel(my_model);
ui->my_table->setModel(my_filter);
我想添加一个新行,如下所示:

my_model->add_item(item);
ui->my_table->reset();
这几乎可以解决一个问题。项目被添加到我的表格中的正确位置并正确显示,但最后一行总是消失。 我哪里做错了什么?为什么最后一排总是消失

谢谢

更新:

 void add_row(Item i){
  int row = rowCount(QModelIndex())+1;
  beginInsertRows(QModelIndex(), row, row);
  items.push_back(i);

  endInsertRows();
  emit dataChanged(createIndex(0,0), createIndex(row,5));
}
此操作有效,但始终返回以下错误:
QSortFilterProxyModel:源模型报告的插入行无效

没有FilterProxyModel,一切都可以找到。FilterProxyModel中缺少了什么?

不确定这是否是问题所在,但我不相信您可以为dataChanged信号使用空QModelIndex值。请尝试为在最后一行中更改并覆盖所有列的起始行发出正确的索引。1-插入行不会更改数据,2-您的行应该是项目的计数,而不是默认构造模型索引的行。@goug I将dataChanged更改为
dataChanged(createIndex(0,0),createIndex(row+1,columnCount)
这似乎可行,但视图更新确实需要很长时间(因为我更新了所有内容?),但如何获取新行的索引?(我使用的是筛选器,因此索引不匹配)@dtech“您的行应该是项目的计数”是什么意思?int行是我的表中的实际行数。您是否尝试在不使用MyQSortFilterProxyModel的情况下使用real
MyTableModel
作为表视图?是否有效?
 void add_row(Item i){
  int row = rowCount(QModelIndex())+1;
  beginInsertRows(QModelIndex(), row, row);
  items.push_back(i);

  endInsertRows();
  emit dataChanged(createIndex(0,0), createIndex(row,5));
}