Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Open a specific path in QTreeView



  • Hi, I have a QTreeView which shows directory and file contents. I wanted to open QTreeView for a specific path like "c:\users\administrator" as a default. How should I do that? There is code sample for this job?

    This is my code to show contents in 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);
    
        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);
    }
    

  • Lifetime Qt Champion

    Iterate through the model data and call expand() on the view for the indexes you want.


Log in to reply