本文介绍了QStandardItemModel :: removeRows()在我的用例中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想删除模型中的所有行.我希望使用removeRows(..)而不是clear(),因为我想保留标题.

Basically I want to delete all rows in my model. I'd prefer to use removeRows(..) instead of clear(), because I want to keep my headers.

我想我已经错过了一些东西(在此处添加文档) ,我的代码非常简单:

I guess I have missed something (docu here), my code is pretty simple:

int c = MainWindow::_viewDataModel->rowCount();
bool r = MainWindow::_viewDataModel->removeRows(0, c);

c例如为4,但r始终为false.我已经尝试将0和1作为第一个索引.使用clear()可行.上面的代码完全没有影响.

c e.g. is 4, but r is always false. I have tried 0 and 1 as first index. Using clear() works. The above code has no impact at all.

推荐答案

它现在正在工作.经过一些测试后,我的发现:

It is working now. After some testing my findings:

    当我指定要删除的模型中的行较少时,
  • RemoveRows非常敏感.
  • 在我的特殊情况下,确实发生了我只想在模型中还剩下4行的情况下想删除5行的情况.因此,如果模型中只有3行,_viewDataModel->removeRows(0, 4);根本不会删除任何内容.
  • 与预期的情况(在这种情况下完全没有删除)一样.
  • 我现在的解决方案是在删除之前彻底获取模型大小.
  • RemoveRows is very sensitive when there are less rows in the model as I do specify for deletion.
  • In my particular case it did happen that I wanted to delete 5 rows when only 4 rows were still in the model. So _viewDataModel->removeRows(0, 4); does not remove anything at all if there are just 3 rows in the model.
  • Unlike expected in such as case no deletion took place at all.
  • My solution right now is to thoroughly get the model size before deleting.

这篇关于QStandardItemModel :: removeRows()在我的用例中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 15:51