我的是 自定义的 模型。 3楼给力。可以 正常显示了。
但是出现了 一个bug. 就是正常更新显示后
如果先操作一个删除操作, 然后在做一个增加操作
这个时候 点击 + 号展开, 程序会 挂掉。
如果去掉 beginInsertRows() , endInsertRows(), beginRemoveRows(), endRemoveRows()
则不会出现该问题。
也就是说,这个新问题是 由于 增加了 这两行代码所致。
下面是我的代码
//这是加结点的代码
int currentRowCount = this->rowCount(parent);
QModelItem *parentItem = static_cast<QModelItem*>(parent.internalPointer());
if(parentItem)
{
parentItem->node().appendChild(temp[0]);
QModelItem *item = parentItem->child(currentRowCount);
if(item)
{
QModelIndex newIndex = this->createIndex(currentRowCount,0,item);
this->beginInsertRows(newIndex.parent(), currentRowCount, currentRowCount);
this->insertRow(currentRowCount,newIndex);
this->endInsertRows();
return newIndex;
}
}
//这是删结点的代码
QModelItem *item = static_cast<QModelItem*>(index.internalPointer());
if(item)
{
QDomNode node = item->node();
QDomNode oldNode = node.parentNode().removeChild(node);
item->parent()->removeChild(index.row());
if(oldNode.isNull() == false)
{
this->beginRemoveRows(index.parent(), index.row(), index.row());
bool rtn = this->removeRow(index.row(), index);
this->endRemoveRows();
return rtn;
}
}
所以想诸位 看看,是不是 哪里参数 设置错了。