user12317345
user12317345

Reputation:

Showing Only Shared Drive and Folders in QTreeView

I have written the following code which can show the shared folder based UNC path in QTreeView widget. However, QTreeView shows a shared folder with my local drive contents. I wanted to remove local drive from that representation. How should I do that?

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

How should I fix this issue in my program? I wanted only show shared drives and folders in QTreeView with by using UNC/CIFS of windows.

Upvotes: 1

Views: 187

Answers (1)

ypnos
ypnos

Reputation: 52397

Implement a QSortProxyFilterModel as an intermediate between filesystem model and the tree view.

E.g. like this:

class FilterSharedFoldersModel : QSortFilterProxyModel {
protected:
    bool filterAcceptsRow(int row, const QModelIndex &parent) const override;
};

Now in the implementation of filterAcceptsRow(), check the type of the respective path and return true or false respectively. See the method documentation as well as the example tutorial.

The wiring is rather easy:

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

Upvotes: 2

Related Questions