美好的一天,我有从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;
}
但是我不能这样做,因为我的模型是单一的,它使用了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
}
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*)) ));
}
鉴于此,我调用了方法:
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();
}
}
}
删除行完美无缺,但插入不起作用。我的实施有什么问题?
答案 0 :(得分:4)
尝试
beginInsertRows(QModelIndex(), 0, item->childCount()-1);
您是否检查过QT doc http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html或QT示例以获取任何线索http://qt-project.org/doc/qt-4.8/itemviews-editabletreemodel.html?
正如你所说的那样,也许这可能是有趣的: