我就想删除topLevelItem stackoverflow上是这样说的:
http://stackoverflow.com/questions/9392051/how-do-i-delete-a-top-level-qtreewidgetitem-from-a-qtreewidget
http://stackoverflow.com/questions/9986231/getting-a-qtreewidgetitem-list-again-from-qtreewidget
以下代码ProjDataView这个类只是单纯继承QTreeWidget
1 #include "ProjDataView.h" 2 3 4 5 6 int main(int argc, char *argv[]) 7 { 8 QApplication a(argc, argv); 9 10 11 ProjDataView* dataView = new ProjDataView(0); 12 13 for (int i = 0; i < 5; ++i){ 14 15 QTreeWidgetItem* item = new QTreeWidgetItem(QStringList() << QString("test") + QString::number(i)); 16 17 18 dataView->addTopLevelItem(item); 19 20 } 21 22 23 24 25 for (int i = 0; i < dataView->topLevelItemCount(); ++i){ 26 27 delete dataView->takeTopLevelItem(i); 28 29 } 30 31 32 33 34 dataView->show(); 35 36 37 38 39 40 41 //MyManagerWidget* mng = new MyManagerWidget(0); 42 43 44 //mng->show(); 45 46 return a.exec(); 47 48 }
如果用上述的代码是不对的,所以从窗口中看出才删除了test0,test2,test4.
如果要全删除的话可以这样:
while(dataView->topLevelItemCount() > 0){delete dataView->topLevelItem(0);}
那个for里边每执行一次dataView->topLevelItemCount()都会变化,
相应的dataView->topLevelItem(i),也是会变化的。完全不对。
不过我最终考虑用clear
参照这个:http://stackoverflow.com/questions/13062327/how-to-delete-qtreewidgetitem
references:
http://tieba.baidu.com/p/3849541021?pid=70405672647#70405672647