[SOLVED]QFileSystemModel set root path
-
*@@
-
@QFileSystemModel* fsModel = new QFileSystemModel(this);
fsModel->setRootPath("c:/file qt/");
fsModel->setReadOnly(true);
fsModel->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
QStringList filter;
filter<<"*.txt";
fsModel->setNameFilters(filter);@ -
No need to post your code again, you can simply edit your answer to add the coding tags
-
[quote author="SGaist" date="1413280688"]You need to also call setRootIndex in your tree view. You have the details in the documentation from QFileSystemModel[/quote]
now it works , thanks a lot , all i added was :
@
ui->treeView->setRootIndex(files->index(fPath));
@
after you set the tree view model -
I'm sorry . are new .
-
You're welcome !
No problem, everybody falls for that one ;)
Happy coding !
-
If you are using treeview can also try qDirModel
@ QStringList filters;
filters << "*.txt";
model = new QDirModel(this);
model->setReadOnly(false);
model->setReadOnly(true);
model->setSorting(QDir::DirsFirst |QDir::IgnoreCase | QDir::Type);
model->setFilter(QDir::AllDirs | QDir::AllEntries |QDir::NoDotAndDotDot);
model->setNameFilters(filters);ui->treeViewDirectory->setModel(model); QModelIndex index=model->index("C:/file qt/"); ui->treeViewDirectory->expand(index); ui->treeViewDirectory->scrollTo(index);@
-
QDirModel has been obsoleted, QFileSystemModel is the right class to use
-
ok ok
-
What if the view is QML ListView and has no setRootIndex()?
QFileSystemModel is badly designed, it makes wrong assumptions on functions you have to be able to call on the view.