2
votes

I have a list of items displayed in a QTableWidget, each one corresponding to a specific folder.

Next to this, I have a QTreeView, with a QFileSystemModel. When I select an item from the QTableWidget, it calls a slot (shown below) in order to display the corresponding folder content.

void MyWidget::diplayFolder(int row)
{
    if (auto item = table->item(row, 1))
    {
        QString correspondingDirectory = item->text();
        if (QDir(correspondingDirectory).exists())
        {
            // treeModel => QFileSystemModel
            // tree      => QTreeView
            treeModel->setRootPath("");
            treeModel->setRootPath(correspondingDirectory);
            tree->setRootIndex(treeModel->index(correspondingDirectory));
        }
        else
        {
            qDebug() << "Reset tree => do not display anything!";
            // treeModel->setRootPath("");
            // tree->reset();
        }
    }
}

If the directory does not exist, I don't want to display anything. However, when I try to set an empty root path or reset the view, it show all my computer drives.

How can I reset or clear the QTreeView ?

1
Could you set the root path to an empty directory you control? - Georg Schölly
@GeorgSchölly Setting the root path to an empty directory works, but I was looking for a cleaner solution... - Arnaud

1 Answers

5
votes

Had a similar issue. Im not quite sure how i solved it, because it was a long time ago. I think it should work if you set the QTreeview a nullptr as model. So when the QDir exists you set a new QFileSystemModel otherwise you call:

tree->setModel(nullptr);

Hope this helps you.

EDIT: If your doing it this way the header is deleted too.