我正在尝试构建一个包含多个QTreeWidgetItem的QTreeWidget,我尝试手动添加其中的一些并且它可以工作。我的问题是如何使用for或while循环添加项目。
这是我的代码的一部分
Dwidget= new QDockWidget(this);
Dwidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
treeWidget= new QTreeWidget(Dwidget);
Titem= new QTreeWidgetItem(treeWidget);
Titem1= new QTreeWidgetItem();
Titem2= new QTreeWidgetItem();
Titem3= new QTreeWidgetItem();
Titem->setText(0,"WriterIdenSystem");
Titem->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/data_repository_icon.jpg")));
Titem1->setText(0,"Database for Writer Identification");
Titem1->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/card_file.png")));
Titem2->setText(0,"0001");
Titem2->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/Folder Open.png")));
Titem3->setText(0,"0002");
Titem3->setIcon(0,*(new QIcon("D:/Users/200656336/Documents/Writer Identification/Folder Open.png")));
Titem->addChild(Titem1);
Titem1->addChild(Titem2);
Titem1->addChild(Titem3);
treeWidget->addTopLevelItem(Titem);
connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(on_actionRetrieve_Documents_triggered()));
Dwidget->setWidget(treeWidget);
addDockWidget(Qt::LeftDockWidgetArea,Dwidget);
Dwidget->show();
任何想法? :)
答案 0 :(得分:0)
根据你的代码,可以做这样的事情。
QTreeWidgetItem* items[ITEM_COUNT]; // Must be multiple of 3
QTreeWidget* treeWidget = new QTreeWidget;
if(!treeWidget){ /* operating system out of memory, handle the error */ }
for(int i=0;i<=(ITEM_COUNT-3);i+=3)
{
items[i ] = new QTreeWidgetItem(treeWidget);
if(!items[i]){ /* operating system out of memory, handle the error */ }
items[i+1] = new QTreeWidgetItem(treeWidget);
items[i+2] = new QTreeWidgetItem(treeWidget);
/* set text here */
items[i ]->addChild(items[i]);
items[i+1]->addChild(items[i+1);
items[i+1]->addChild(items[i+2);
treeWidget->addTopLevelItem(items[i]);
}