在QTreeView中插入和删除行

Top*_*ndr 4 c++ qt qtreeview qabstractitemmodel

美好的一天,我有一个继承自QAbstractItemModel的基本模型,还有一些不时通知该模型的后台线程,在示例中,插入行实现了这样的功能

bool TreeModel::insertRows(int position, int rows, const QModelIndex &parent)
{
    TreeItem *parentItem = getItem(parent);
    bool success;

    beginInsertRows(parent, position, position + rows - 1);
    success = parentItem->insertChildren(position, rows, rootItem->columnCount());
    endInsertRows();

    return success;
} 
Run Code Online (Sandbox Code Playgroud)

但是我不能这样做,因为我的模型是单个的,它使用4个视图,所以我是这样实现插入的:

void notifyEventImpl(file_item_type *sender,helper<ITEM_ACTION_ADDED>)
        {
            base_class::setSize(file_item_type::size()+sender->size());         
            m_listDirectory.push_back(sender);
            file_item_type::filesystem_type::s_notify.insert(this); // notify my model
        } 
Run Code Online (Sandbox Code Playgroud)

s_notify有实现的类在哪里:

 void Notifaer::dataChange(void * item){emit dataChanged(item);}
        void Notifaer::remove(void * item){emit removed(item);}
        void Notifaer::insert(void * item){emit inserted(item);}
        void Notifaer::push_back(const FileItemModel * model)
        {
            VERIFY(QObject::connect(this,SIGNAL(dataChanged(void*)),model,SLOT(dataChangeItem(void*)) ));
            VERIFY(QObject::connect(this,SIGNAL(removed(void*)),model,SLOT(removeItem(void*)) ));
            VERIFY(QObject::connect(this,SIGNAL(inserted(void*)),model,SLOT(insertItem(void*)) ));
        }
Run Code Online (Sandbox Code Playgroud)

鉴于此,我将调用该方法:

void FileItemModel::insertItem(void *it)
{
    file_item_type *item = dynamic_cast<file_item_type*>(static_cast<file_item_type*>(it));

    {
        QModelIndex index = createIndex(0,0,item);
        if (index.isValid())
        {
            beginInsertRows(index, 0, item->childCount()-1);
            endInsertRows();
        }
    }
}
void FileItemModel::removeItem(void *it)
{
    file_item_type *item = static_cast<file_item_type*>(it);

    {
        QModelIndex index = createIndex(0,0,item);
        if (index.isValid())
        {
            beginRemoveRows(index, 0, item->childCount()-1);
            endRemoveRows();
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

删除行效果很好,但插入行不起作用。我的实现有什么问题?

kik*_*que 5

试试看

 beginInsertRows(QModelIndex(), 0, item->childCount()-1);
Run Code Online (Sandbox Code Playgroud)

您是否检查过QT doc http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html或QT示例以获取任何线索http://qt-project.org/doc/qt-4.8/itemviews-editabletreemodel .html

正如您所说的线程,也许这可能很有趣: