我正在尝试为QTreeWidgetItem设置新的父级,下面是我的代码:
1.if( it->parent() )//'it' is QTreeWidgetItem
2. {
3. QTreeWidgetItem* parent = it->parent();
4. parent->takeChild(parent->indexOfChild(it));
5. }
7. under->addChild( it );//'under' is new parent of 'it'在第四行程序之后,read access violation at 0x0将失败。
编辑的
Q_CHECK_PTR(under);
Q_CHECK_PTR(it);
if( it->parent() )
{
QTreeWidgetItem* parent = it->parent();
Q_CHECK_PTR(parent);
Q_ASSERT( parent->child( parent->indexOfChild(it) ) == it );
parent->removeChild(it);
//or
//it = new QTreeWidgetItem( *(parent->takeChild(parent->indexOfChild(it))) );
//or
//it = parent->takeChild(parent->indexOfChild(it));
//or
//parent->takeChild(parent->indexOfChild(it));
}
Q_CHECK_PTR(under);
Q_CHECK_PTR(it);
under->addChild( it );同样的结果。
发布于 2015-11-03 12:36:05
试试下面的代码:
QTreeWidgetItem *parent = it->parent();
parent->removeChild(it);
under->addChild(it); 基本使用removeChild insted of takeChild
更新
removeChild从父项中移除给定项,并且不返回anyting。请注意,删除的项目将不会被删除。但是takeChild返回子指针。
https://stackoverflow.com/questions/33498640
复制相似问题