PDA

View Full Version : How display only /tmp dir with QFileSystemModel and QTreeView



newbie43
15th July 2010, 16:11
Hi
Im having some problems with displaying a directory and its files in a QTreeView, im using QFileSystemModel and i keep getting a directory listing that looks like this

/
/tmp
somefile
+opt
+root
+sys
someotherfile

etc, etc, all i want to be displayed is the /tmp directory and its files. Im trying to make it so the user cant navigate anywhere else but the /tmp directoy. Here is the way im doing it now which keeps giving me all the files under /


QString Location("/tmp/delete");
m_FsModel = new QFileSystemModel();
m_FsModel->setRootPath(Location);

QDir* dir = new QDir(Location);
m_FsModel->setRootPath(dir->path());
m_FsModel->setReadOnly(true);

thanks

qjack
18th July 2010, 21:54
You want only files and sub directories from /tmp/ listed? Then I wonder why you use

QString Location("/tmp/delete");as root path.
Shoudn't it be
String Location("/tmp/");?

ChrisW67
19th July 2010, 00:32
The docs for QFileSystemModel::setRootPath() contain this note:

Note: This function does not change the structure of the model or modify the data available to views. In other words, the "root" of the model is not changed to include only files and directories within the directory specified by newPath in the file system.
Emphasis mine. You probably want to look up the model index corresponding to "/tmp" and call QTreeView::setRootIndex()


QFileSystemModel *m_FsModel = new QFileSystemModel();
m_FsModel->setRootPath("/tmp");

QTreeView v;
v.setModel(m_FsModel);
QModelIndex idx = m_FsModel->index("/tmp");
v.setRootIndex(idx);
v.show();

newbie43
17th August 2010, 18:58
That was perfect thanks!