List folders with Qtreewidget in Qt c++

2.1k views Asked by At

I'm listing the folder using QTreeWidget. I wrote the following codes and it looks like the picture.

But, I do not want it to be displayed like this. I want to add a box icon and add a dashed line to the left side of the box. I added a picture below the code that I wanted to do. How can it be displayed like this.

enter image description here

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

I want it to be as follows

enter image description here

1

There are 1 answers

0
Elcan On BEST ANSWER

There are 2 possible solutions (code from http://www.qtcentre.org/threads/9155-how-to-add-icons-to-QTreeWidget) :

Set the icon during runtime on your item :

QTreeWidget Tree;
QTreeWidgetItem *item ;
item = new QTreeWidgetItem(Tree);
item->setText(0, text);
item->setIcon(0, QIcon("your icon path or file name "));
item->setExpanded( true );

Custom Model :

If you want more freedom, you will have to create your own implementation of QAbstractItemModel, because your nodes will have different graphical states depending on the data inside them.

Good tutorial to start with that : http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

Where to put your decoration :

QVariant YourModel::data(const QModelIndex &index, int role) const
{
    ...
    switch (role)
    {
        ...
        case Qt::DecorationRole: // <---
            return QIcon(...); // <---
        ...
    }
    ...
}

For the box icon

This theme is shipped with basic Windows Forms programs' TreeView control, as it is the old look of the tree view controls. There might be some way to trigger it, as it exists inside Windows, but I have no idea how.