Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileSystemModel update problem for shared network location
Qt 6.12 LTS is out! See what's new in the release blog

QFileSystemModel update problem for shared network location

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 971 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

    I'm using QFileSystemModel and QTreeView to display files tree from a shared network location.

    void MainWindow::ListDirectory(QString arg_smb_path)
    {
        o_directory_system_model = new QFileSystemModel(this);
        o_directory_system_model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
        o_directory_system_model->setRootPath(arg_smb_path);
    
        ui->treeView->setModel(o_directory_system_model);
        ui->treeView->hideColumn(1);
    
        o_file_system_model = new QFileSystemModel(this);
        o_file_system_model->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden);
        o_file_system_model->setRootPath(arg_smb_path);
    
        ui->listView->setModel(o_file_system_model);
    
        ui->listView->setContextMenuPolicy(Qt::CustomContextMenu);
    }
    

    The code works fine and I got the file tree. However, it can not automatically pick up the changes there. For example, if I create a new file there, the tree don't display this new file. When I set the path to a directory on local hard drive, like "D:/temp". It works. It picks up the changes and update itself. Any idea how can I pick up changes from a shared network location and refresh the model/tree?

    JonBJ 1 Reply Last reply
    0
    • AzadshahrA Azadshahr

      I'm using QFileSystemModel and QTreeView to display files tree from a shared network location.

      void MainWindow::ListDirectory(QString arg_smb_path)
      {
          o_directory_system_model = new QFileSystemModel(this);
          o_directory_system_model->setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Hidden);
          o_directory_system_model->setRootPath(arg_smb_path);
      
          ui->treeView->setModel(o_directory_system_model);
          ui->treeView->hideColumn(1);
      
          o_file_system_model = new QFileSystemModel(this);
          o_file_system_model->setFilter(QDir::NoDotAndDotDot | QDir::Files | QDir::Hidden);
          o_file_system_model->setRootPath(arg_smb_path);
      
          ui->listView->setModel(o_file_system_model);
      
          ui->listView->setContextMenuPolicy(Qt::CustomContextMenu);
      }
      

      The code works fine and I got the file tree. However, it can not automatically pick up the changes there. For example, if I create a new file there, the tree don't display this new file. When I set the path to a directory on local hard drive, like "D:/temp". It works. It picks up the changes and update itself. Any idea how can I pick up changes from a shared network location and refresh the model/tree?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Azadshahr
      This is a common problem for network locations for many operating systems/file systems. Not just Qt, e.g. you can often see this (lack of update) behaviour in, say, a File Explorer on a network path.

      If you think about it, you can see the host program may have trouble knowing that a remote network location has changed, it can't afford to poll all the time.

      I don't know whether QFileSystemModel already uses this internally, but you should try a QFileSystemWatcher on the network directory. If that does not notify of changes, it's not going to work. In that case, I would suggest you can only put in a QTimer and periodically poll the remote to spot any changes, which is not great. You may well find that whether it works or not depends on the actual file system/network share protocol. Otherwise, I don't know if there is a Windows-only call you could use which can see the network update if the Qt code does not.

      1 Reply Last reply
      0

      • Login

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