c++ - 在QTreeView中仅显示共享驱动器和文件夹

标签 c++ qt

我编写了以下代码,可以在QTreeView小部件中显示基于共享文件夹的UNC路径。但是,QTreeView显示包含我的本地驱动器内容的共享文件夹。我想从该表示中删除本地驱动器。我该怎么办?

void MainWindow::ListDirectory(QString arg_smb_path)
{
    o_directorySystemModel = new QFileSystemModel(this);
    o_directorySystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
    o_directorySystemModel->setRootPath(arg_smb_path);

    ui->treeView->setModel(o_directorySystemModel);
    ui->treeView->hideColumn(1);
    ui->treeView->hideColumn(2);
    ui->treeView->hideColumn(3);


    o_fileSystemModel = new QFileSystemModel(this);
    o_fileSystemModel->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden);
    o_fileSystemModel->setRootPath(arg_smb_path);

    ui->listView->setModel(o_fileSystemModel);
    ui->listView->setContextMenuPolicy(Qt::CustomContextMenu);
}
如何在程序中解决此问题?我只想通过Windows的UNC / CIFS在QTreeView中显示共享驱动器和文件夹。

最佳答案

实现QSortProxyFilterModel作为文件系统模型和树 View 之间的中介。
例如。像这样:

class FilterSharedFoldersModel : QSortFilterProxyModel {
protected:
    bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
};
现在在filterAcceptsRow()的实现中,检查相应路径的类型并分别返回true或false。参见method documentationexample tutorial
接线非常简单:
o_directorySystemModel = new QFileSystemModel(this);
o_directorySystemModel->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
o_directorySystemModel->setRootPath(arg_smb_path);
o_directoryFilterModel = new FilterSharedFoldersModel();
o_directoryFilterModel->setSourceModel(o_directorySystemModel);
ui->treeView->setModel(o_directoryFilterModel);

关于c++ - 在QTreeView中仅显示共享驱动器和文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62657864/

相关文章:

c++ - 如何在不同的类中使用函数指针?

c++ - QGeoPath 与 QGeoPolygon

c++ - 如何清除 QTreeView/QFileSystemModel

c++ - 使用 Qt 树模型存储数据?

c++ - 在类函数中重载按位 And(&) 运算符和 "*this"

c++ - Windows 7 上的 C++ 程序中的链接器错误

qt - 根据 Qt 中的固定小部件调整视频大小

c++ - QT 中的信号槽

c++ - C++ 标准要求 new char[] 产生对齐内存的意图是什么?

c++ - 计算不同类别的对象数量