我有一个treeWidget,我需要删除QTreeWidgetItem的childelement。
QTreeWidgetItem *listing = new (ui->treeWidget);
AddChild(listing,id);
AddChild(listing,id);`// this is what i need to delete
AddChild(listing,id);
listing->takeChild(1);
我尝试了func takeChild(int index),但没有发生任何事情
void AddChild(QTreeWidget *s,int id)
{
QTreeWidgetItem *_s = new QTreeWidgetItem(ui->treeWidget);
_s->setText(0,QString::number(id));
s->addChild(_s);
}
答案 0 :(得分:0)
您的代码中存在错误,
QTreeWidgetItem *listing = new (ui->treeWidget);
应该是:
QTreeWidgetItem *listing = new QTreeWidgetItem(ui->treeWidget);
并且AddChild()
函数的签名是错误的:
void AddChild(QTreeWidget *s,int id)
应该是:
AddChild(QTreeWidgetItem *s,int id)
现在takeChild()
无效的主要问题是,您不是将这些项目作为子项添加到listing
,而是将它们添加到treeWidget
,因此在AddChild()
中,您应该将父级设置为S
listing
,所以您的代码应为:
QTreeWidgetItem *listing = new QTreeWidgetItem(ui->treeWidget);
listing->setText(0, tr("Listing"));
AddChild(listing,0);
AddChild(listing,1); // this is what i need to delete
AddChild(listing,2);
listing->takeChild(1);
和AddChild()函数
void AddChild(QTreeWidgetItem *s,int id)
{
QTreeWidgetItem *_s = new QTreeWidgetItem(s);
_s->setText(0,QString::number(id));
s->addChild(_s);
}