簡體   English   中英

如何清除QTreeView / QFileSystemModel

[英]How to clear a QTreeView / QFileSystemModel

我在QTableWidget中顯示了一個項目列表,每個項目都對應一個特定的文件夾。

接下來,我有一個QTreeView和一個QFileSystemModel。 當我從QTableWidget中選擇一個項目時,它會調用一個插槽(如下所示)以顯示相應的文件夾內容。

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

如果該目錄不存在,那么我什么也不想顯示。 但是,當我嘗試設置空的根路徑或重置視圖時,它將顯示我的所有計算機驅動器。

如何重置或清除QTreeView?

發生了類似的問題。 我不太確定我是怎么解決的,因為那是很久以前的事了。 我認為,如果將QTreeview設置為nullptr作為模型,它應該可以工作。 因此,當QDir存在時,請設置一個新的QFileSystemModel,否則調用:

tree->setModel(nullptr);

希望這對您有幫助。

編輯:如果您這樣做,標題也將被刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM