PDA

View Full Version : Populate a QTreeView from a string like it was a local directory



dotjan
14th February 2012, 22:18
Hi all,
I have a QTreeView widget where I list the content of a local directory, I use the QFileSystemModel object for this, and I have a list of directory in a file in the following form:

...
testfile1
testfile2
dir1/
dir1/testfile1
dir1/testfile2
dir2/dir1/testfile1
dir2/dir1/testfile2
...

My need is to get this list into a QStringList object and then _somehow_ use is to show it as in another QTreeWidget like if it was a local directory.

It is a very ambitious idea considering my basic skills, hopefully not that much for some of you, I do not even know if this is actually possible..

Thanks,
Jan

KillGabio
15th February 2012, 03:51
Hi in this case it`s more like a programming challenge...What I would do in my case is to have a matrix: colums indicate directory and rows the files...in your example it would be something like:

[root ][dir1 ][dir2 ]
[test1][test1][test1]
[test2][test2][test2]

So yo read the lines till a / and you know that if it has no extension its a dir, bla bla bla

dotjan
16th February 2012, 17:05
Hi, not sure this is only a programming challenge. I would have said this something around subclassing QFileSystemModel, but I am not sure about it and honestly still need to figure it out. What I want is to show the a list of file contained in a file with the above syntax as it was a file system. I was able to create a directory structure with empty files on a temporary directory and use this one as rootPath for the model, but this is just a workaround to me and cannot be considered a good option as it requires times and space on disk, which is something I would like to avoid.

Any other suggestion?

Thanks,
Jan

wysota
16th February 2012, 21:08
Hi all,
I have a QTreeView widget where I list the content of a local directory, I use the QFileSystemModel object for this, and I have a list of directory in a file in the following form:

...
testfile1
testfile2
dir1/
dir1/testfile1
dir1/testfile2
dir2/dir1/testfile1
dir2/dir1/testfile2
...

My need is to get this list into a QStringList object and then _somehow_ use is to show it as in another QTreeWidget like if it was a local directory.
What exactly is the purpose of doing that?

dotjan
16th February 2012, 22:10
Basically that is the output of the rsync command from a remote host, it runs in dry-run mode to checks for changed files which needs to be synchronized.

# rsync -anuv /tmp/2/ /tmp/1
sending incremental file list
./
file
file2
dir1/
dir1/file1
dir1/file2
dir1/file3
dir2/
dir2/file1
dir2/file2
dir2/file3

sent 257 bytes received 47 bytes 608.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)

The stdout is properly formatted and stored in a QStringList which is then used to set the QStringListModel:



// get list of files on the remote host which needs to be synchronized
QStringList destList = remote->newOnDest(remoteHost, srcDir, destDir, type);
if (!srcList.isEmpty()) { // initialize model only if there is something new
QStringListModel *destModel = new QStringListModel;
destModel->setStringList(destList);
ui->listView_dest->setModel(destModel);

As said I would like to use QFileSystemModel so that 1)the view is much more nice and 2) user can browse through folders. Let me know if I could provide further info.

Thanks,
Jan

wysota
16th February 2012, 22:45
So you want to mimic a directory tree? This is quite easy using QStandardItemModel. For each line of your output divide the path into components and use a hash of QStandardItem* instances to find parents of the element you are currently trying to position and simply attach the newly created item to the parent found. In the end you'll end up with a model consistent with your rsync output.

dotjan
20th February 2012, 10:59
Yes right, I would need to mimic a directory trees and presenting it just like the QFileSystemModel does. I have not been able to try this out, I will let you know.. TIA