将项目添加到根目录时,QTreeview 无法正确更新 [英] QTreeview does not update correctly when items are added to the root

查看:37
本文介绍了将项目添加到根目录时,QTreeview 无法正确更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个树视图,它应该显示自己的数据集合的内容来实现这一点,我为树视图实现了一个新模型.当我将一个对象添加到任何父项时,一切正常并且新项显示在视图中,但是当我尝试向根项添加一个项时,该项不会显示,直到我将一个对象添加到另一个父项或我重置模型.

I've got a treeview, which should show the content of an own datacollection to achieve this, I've implemented a new model for the treeview. When I add an object to any parent Item everything works fine and the new Item is shown in the view, but when I try to add an item to the rootitem, this item does not show up until I add an Object to another parentitem or I reset the model.

我向模型添加行的方法如下所示:

My method to add rows to the model looks like this:

bool TreeModel::insertRows(int row, int count, const QModelIndex &parent, DataObject *object, QString name, QString path)
{
  if (!parent.isValid())
     return false;

  DataCollection* dataCollection = static_cast<DataCollection*>(parent.internalPointer());

  beginInsertRows(parent, dataCollection->Size(), dataCollection->Size());
  dataCollection->AddData(object, name.toStdString(), path.toStdString());
  endInsertRows();


  return true;
}

如何在向根元素添加项目时实现视图的更新?

How can I achieve an update of the view when I am adding an item to the rootelement?

推荐答案

我找到了一个非常简单的方法来解决这个问题 - 你所要做的就是添加emit layoutChanged(); 到你的 insertRows() 函数.

I found a really easy way to solve this - all you have to do is add emit layoutChanged(); to your insertRows() function.

最初我发出 dataChanged() 但显然它只影响项目的数据,与分支相关的数据不是项目的一部分.

Initially I was emitting dataChanged() but apparently it only affects the data of the item, the branch-related data is not part of an item.

它不如正确使用 beginInsertRows() 和 endInsertRows() 有效,但如果您拥有的项目数量不多,此解决方案应该绰绰有余.

It's not as efficient as doing it properly with beginInsertRows() and endInsertRows() but if the amount of items you have is not massive this solution should be more than sufficient.

这篇关于将项目添加到根目录时,QTreeview 无法正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆