0
我正在使用QTreeWidget列出文件夾。我寫了下面的代碼,它看起來像圖片。在Qt c + +中使用Qtreewidget列出文件夾
但是,我不希望它像這樣顯示。我想添加一個框圖標,並在框的左側添加一條虛線。我在我想要做的代碼下面添加了一張圖片。它怎麼能這樣顯示。
QTreeWidgetItem *topLevelItem = NULL;
foreach (const QString &fileName, fileNames)
{
QList<QString> splitFileName;
splitFileName.append(fileName.split("/"));
// add root folder as top level item if treeWidget doesn't already have it
if (ui->treeWidget->findItems(splitFileName[0], Qt::MatchFixedString).isEmpty())
{
topLevelItem = new QTreeWidgetItem;
topLevelItem->setText(0, splitFileName[0]);
ui->treeWidget->addTopLevelItem(topLevelItem);
}
QTreeWidgetItem *parentItem = topLevelItem;
// iterate through non-root directories (file name comes after)
for (int i = 1; i < splitFileName.size() - 1; ++i)
{
// iterate through children of parentItem to see if this directory exists
bool thisDirectoryExists = false;
for (int j = 0; j < parentItem->childCount(); ++j)
{
if (splitFileName[i] == parentItem->child(j)->text(0))
{
thisDirectoryExists = true;
parentItem = parentItem->child(j);
break;
}
}
if (!thisDirectoryExists)
{
parentItem = new QTreeWidgetItem(parentItem);
parentItem->setText(0, splitFileName[i]);
}
}
if(splitFileName.last()!="")
{
QTreeWidgetItem *childItem = new QTreeWidgetItem(parentItem);
childItem->setText(0, splitFileName.last());
}
}
fileNames.clear();
}
我想這是如下