[SOLVED]QFileSystemModel set root path
-
[quote author="Vovasty" date="1413279412"]Hi,
maybe following link helps:
"Link":http://qt-project.org/forums/viewthread/1846[/quote]
sorry but my problem is treeview doesn't even set my directory , i dont care if other directories also displayed . -
It's an invalid path. A backslash in a strings is an escape character. Either escape your backslashes (use \ in your string) or since you are using Qt, use the unix notation: forward slashes, less error prone.
-
[quote author="SGaist" date="1413279766"]It's an invalid path. A backslash in a strings is an escape character. Either escape your backslashes (use \ in your string) or since you are using Qt, use the unix notation: forward slashes, less error prone.[/quote]
it helped with recognizing the directory (the message now pops up) , but not with the root tree view path (back as original post problem)
-
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);
-
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); -
You need to also call setRootIndex in your tree view. You have the details in the documentation from QFileSystemModel
-
*@@
-
@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.