3

我有一个项目QTreeWidgetQTreeWidgetItem但是只有根节点显示它的图标:

在此处输入图像描述

我一直在摸索什么可以关闭它,有什么提示吗?

  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);
  }
4

1 回答 1

6

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.

于 2013-02-20T14:55:08.497 回答