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. Failed to get `QModelIndex` for `QFileSystemModel`
Forum Update on Monday, May 27th 2025

Failed to get `QModelIndex` for `QFileSystemModel`

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 293 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.
  • jronaldJ Offline
    jronaldJ Offline
    jronald
    wrote on last edited by
    #1
    #include "MainWindow.h"
    
    #include <QApplication>
    #include <QSplitter>
    #include <QFileSystemModel>
    #include <QTreeView>
    #include <QListView>
    #include <QMessageBox>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QSplitter *splitter = new QSplitter;
    
        QFileSystemModel *model = new QFileSystemModel(splitter);
        model->setRootPath(QDir::currentPath());
    
        QTreeView *tree = new QTreeView(splitter);
        tree->setModel(model);
        tree->setRootIndex(model->index(QDir::currentPath()));
    
        QListView *list = new QListView(splitter);
        list->setModel(model);
        QModelIndex list_root_index = model->index(QDir::currentPath());
        list->setRootIndex(list_root_index);
    
    
        QItemSelectionModel * list_selection_model = list->selectionModel();
    
        splitter->setWindowTitle("Two views onto the same file system model");
        splitter->show();
    
        QModelIndex index = model->index(0, 0, list_root_index);
        bool ok = index.isValid();
        assert(ok);
        list_selection_model->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
    
        return app.exec();
    }
    

    assert failed

    eyllanescE 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Take one thing into account, QFileSystemModel is a threaded model so you might be trying to access it while it's not yet populated.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      jronaldJ 1 Reply Last reply
      3
      • jronaldJ jronald
        #include "MainWindow.h"
        
        #include <QApplication>
        #include <QSplitter>
        #include <QFileSystemModel>
        #include <QTreeView>
        #include <QListView>
        #include <QMessageBox>
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
        
            QSplitter *splitter = new QSplitter;
        
            QFileSystemModel *model = new QFileSystemModel(splitter);
            model->setRootPath(QDir::currentPath());
        
            QTreeView *tree = new QTreeView(splitter);
            tree->setModel(model);
            tree->setRootIndex(model->index(QDir::currentPath()));
        
            QListView *list = new QListView(splitter);
            list->setModel(model);
            QModelIndex list_root_index = model->index(QDir::currentPath());
            list->setRootIndex(list_root_index);
        
        
            QItemSelectionModel * list_selection_model = list->selectionModel();
        
            splitter->setWindowTitle("Two views onto the same file system model");
            splitter->show();
        
            QModelIndex index = model->index(0, 0, list_root_index);
            bool ok = index.isValid();
            assert(ok);
            list_selection_model->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows);
        
            return app.exec();
        }
        

        assert failed

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #3

        @jronald related https://stackoverflow.com/questions/58759075/why-my-qfilesystemmodel-qmodelindex-couldnt-get-child-node-infomation/58759339#58759339, use directoryLoaded signal.

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          Take one thing into account, QFileSystemModel is a threaded model so you might be trying to access it while it's not yet populated.

          jronaldJ Offline
          jronaldJ Offline
          jronald
          wrote on last edited by jronald
          #4

          @SGaist said in Failed to get &#x60;QModelIndex&#x60; for &#x60;QFileSystemModel&#x60;:

          QFileSystemModel is a threaded model so you might be trying to access it while it's not yet populated.

          Tested, it is just the cause of the problem, thanks.

          @eyllanesc said in Failed to get &#x60;QModelIndex&#x60; for &#x60;QFileSystemModel&#x60;:

          use directoryLoaded signal.

          It's what's in need, thanks.

          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 Extensions
          • Unsolved