
现象描述:当在Clip后返回主界面时,Clip后的记录B会从当前的记录A COPY一份,并在记录B下生成子记录B1、B2。关系如下:
A
B
|--B1
|--B2
此时记录B的子节点与B一块显示不出来,若B1,B2直接为记录A的子节点是没有问题,其记录B新增加的方式与已经存在增加addProfile类似。测试若把B当成A的子记录,如下关系:
A
|--B
|--B1
|--B2
这时显示没有问题。
简化代码:
//itemData 相当于B
//item 相当于A, A,B为兄弟节点
itemData = new ImListTreeItem(item);
*itemData = *item; //COPY data
QModelIndex idx = model()->getItemIndex(item);
//得到当前记录的parent, itemData与item的parent相同
ImListTreeItem *parentItem = model()->getItem(idx.parent()); //由索引找到parent记录
parentItem->insertChildren(parentItem->childCount(), itemData); //添加itemData为parentItem子记录
这时在model中的data()跟踪无 itemData对应的index
现在改为:index = model()->addItem(itemData, idx.parent());则正常
QModelIndex ImListTreeModel::addItem(ImListTreeItem *item, const QModelIndex &parentIdx)
{
ImListTreeItem *parent = getItem(parentIdx);
int position = parent->childCount();
beginInsertRows(parentIdx, position, position );
parent->insertChildren(position, item);
endInsertRows();
return index(position, 0, parentIdx);
}
他们的区别也就是:beginInsertRows(parentIdx, position, position );
不知为什么,在根节点下要用beginInsertRows,若非根节点下不用beginInsertRows也能正常为显示记录

在Qt QTreeView中,当尝试将一个记录B(包含子记录B1、B2)COPY到另一个记录A下作为子节点时,记录B及其子节点无法正常显示。问题出现在使用`insertChildren`方法添加数据时,没有调用`beginInsertRows`和`endInsertRows`。在更改代码,调用`addItem`方法并正确使用`beginInsertRows`后,问题得到解决。
&spm=1001.2101.3001.5002&articleId=6048519&d=1&t=3&u=8ab5c9f76b8e4cdaa28970684c23b3bf)
5606

被折叠的 条评论
为什么被折叠?



