Failed to get `QModelIndex` for `QFileSystemModel`
Solved
General and Desktop
-
#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 -
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.
-
#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 -
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.
@SGaist said in Failed to get `QModelIndex` for `QFileSystemModel`:
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 `QModelIndex` for `QFileSystemModel`:
use directoryLoaded signal.
It's what's in need, thanks.