QTreeView和QAbstractItemModel& insertRow [英] QTreeView & QAbstractItemModel & insertRow

查看:416
本文介绍了QTreeView和QAbstractItemModel& insertRow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为QTreeView实现QAbstractItemModel.我在插入行时遇到问题. 我注意到,如果我在应用程序的开头插入所有内容,则一切正常.但是,如果我稍后插入行-在执行某些其他操作(如选择等)之后,新项目将保持不可见.而且QTreeView似乎根本不起作用!我是否必须发出一些信号来通知QTreeView有关行插入?

I'm trying to implement QAbstractItemModel for QTreeView. I have problem with inserting rows. I noticed that if I insert at the beginning of my application all works fine. But If I insert rows later - after some other operations (like selections etc.) new items stay invisible. Moreover QTreeView seems to doesn't work at all! Do I have to emit some signals to notify QTreeView about rows insertion?

这是我的插入方法:

bool LayersModel::insertRows(int position, int count, const QModelIndex  & parent)
{
    LayersModelItem * parentItem = getItem(parent);
    if (position > parentItem->childCount())
        return false;
    beginInsertRows(parent,position,position+count-1);
    bool result = true;
    for (;count;--count)
        result &= parentItem->insertChildren(position, new LayersModelItem());
    endInsertRows();
    return result;
}

LayersModelItem是带有QList及其子项和数据的类.

LayersModelItem is class with QList with its children and data.

我的项目的完整代码(需要KDE库)在这里: https://github.com/coder89/PhotoFramesEditor/tree/v0.0.8 要查看该问题,请在主窗口中选择蓝色项目之一,然后右键单击它,然后选择删除项目". (此方法位于Canvas :: removeItems()中,并且已完全注释-我很绝望,并且我试图找到此问题的原因...(实际上它并没有删除任何内容-它添加了新项目) ).

Full code of my project (KDE libs needed) is here: https://github.com/coder89/PhotoFramesEditor/tree/v0.0.8 To see the problem select one of blue item on main window and then right-click on it and select "Delete item". (this method is in Canvas::removeItems()) and it is completly commented - I'm desperate and I've tried to find reason of this problem... (in fact it wasn't delete anything - it adds new item).

感谢您的任何帮助&建议!

Thanks for any help & advice!

推荐答案

快速猜测一下,用于QAbstractItemModel的QT文档说...

Just a quick guess, the QT Doc for QAbstractItemModel says...

模型发出信号以指示 变化.例如,dataChanged()为 每当产生数据项时发出 可用的型号已更改. 更改由 模型导致headerDataChanged()为 发射. 如果结构 基础数据更改,模型可以 发出* layoutChanged()以指示 他们应该附上的任何附带意见 重新显示显示的所有项目, 新结构 *.

The model emits signals to indicate changes. For example, dataChanged() is emitted whenever items of data made available by the model are changed. Changes to the headers supplied by the model cause headerDataChanged() to be emitted. If the structure of the underlying data changes, the model can emit *layoutChanged() to indicate to any attached views that they should redisplay any items shown, taking the new structure into account*.

所以我想,您需要从模型中发出layoutChanged()信号(无论何时更改模型中的数据),以便更新连接的视图.

So i guess, you need to emit layoutChanged() signal from your model (whenever you change the data in model) in order to update connected views.

还请阅读有关模型视图架构的QT文档,以及如何在QT中实现

Also read the QT docs for model view architecture, how it is implemented in QT

看看是否有帮助,否则,我将尝试下载您的代码并对其进行调试,然后看看有什么问题. 祝你好运

see if that helps, if it doesn't i will try to download your code and debug it and see, what's wrong. Good Luck

这篇关于QTreeView和QAbstractItemModel& insertRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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