简体   繁体   中英

How to clear a QTreeView / QFileSystemModel

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM