插入和删除行

问题描述:

美好的一天,我有示范基地,从化QAbstractItemModel继承,以及一些后台线程用来通知该模型不时,在例子中插入行implemens财产以后这样插入和删除行

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(); 
     } 
    } 
} 

删除行的作品完美,但插不工作。我的实施出了什么问题?

+1

你能提供有关如何插入 “不工作” 的详细信息?它会崩溃吗(如果是这样,错误信息是什么?),什么都不做,或者做别的事情? – James

+1

简单不会出现新行 –

尝试用

beginInsertRows(QModelIndex(), 0, item->childCount()-1); 

你检查QT文档http://qt-project.org/doc/qt-4.8/qabstractitemmodel.html或QT的例子让任何线索http://qt-project.org/doc/qt-4.8/itemviews-editabletreemodel.html

正如你所说的线程,也许这可能是有趣的阅读: