Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Open a specific path in QTreeView
Qt 6.11 is out! See what's new in the release blog

Open a specific path in QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 437 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AzadshahrA Offline
    AzadshahrA Offline
    Azadshahr
    wrote on last edited by Azadshahr
    #1

    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);
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved