Reputation: 12317
I have a QTreeWidget
with QTreeWidgetItem
items, however only the root node is showing its icon:
I've been scratching my head on what could turn it off, any hints?
ui->folderTree1->setUpdatesEnabled( false );
QTreeWidgetItem* treeRoot1 = new QTreeWidgetItem(ui->folderTree1);
treeRoot1->setIcon(0, QIcon(":/icons/black.png"));
treeRoot1->setText(0, tr("Root"));
treeRoot1->setExpanded(true);
addFoldersToTreeView(treeRoot1, ui->filePath1->text(), ui->filePath2->text());
ui->folderTree1->setUpdatesEnabled( true );
}
void MainWindow::addFoldersToTreeView(QTreeWidgetItem* currentWidget, QString leftPath, QString rightPath)
{
qDebug() << "MainWindow::addFoldersToTreeView" << leftPath;
QDir dir(leftPath);
QDir dir2(rightPath);
/* Add the folders */
foreach (QString subDir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))
{
QString sImageName = ":/icons/black.png";
QTreeWidgetItem* newItem = new QTreeWidgetItem(currentWidget);
qDebug() << "MainWindow::addFoldersToTreeView.sImageName" << sImageName;
newItem->setIcon(0, QIcon(sImageName));
newItem->setText(0, subDir);
newItem->setExpanded(true);
newItem->setData(0, 1, QVariant(leftPath + QDir::separator() + subDir));
/* Recursively add sub-folders */
addFoldersToTreeView(newItem, leftPath + QDir::separator() + subDir, rightPath + QDir::separator() + subDir);
}
Upvotes: 3
Views: 583
Reputation: 5827
The problem is the line:
newItem->setData(0, 1, QVariant(leftPath + QDir::separator() + subDir));
The second argument is the item data role, which you specify to 1 (Qt::DecorationRole). The Qt::DecorationRole should be used for data that is rendered as a decoration in the form of an icon, i.e., this line will cause the the icon you specified before to be replaced with a QVariant
object.
Remove that line or change the item data role to something else.
Upvotes: 6