Results 1 to 7 of 7

Thread: Populate a QTreeView from a string like it was a local directory

  1. #1
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Populate a QTreeView from a string like it was a local directory

    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

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Populate a QTreeView from a string like it was a local directory

    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

  3. #3
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: Populate a QTreeView from a string like it was a local directory

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Populate a QTreeView from a string like it was a local directory

    Quote Originally Posted by dotjan View Post
    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: Populate a QTreeView from a string like it was a local directory

    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:

    Qt Code:
    1. // get list of files on the remote host which needs to be synchronized
    2. QStringList destList = remote->newOnDest(remoteHost, srcDir, destDir, type);
    3. if (!srcList.isEmpty()) { // initialize model only if there is something new
    4. destModel->setStringList(destList);
    5. ui->listView_dest->setModel(destModel);
    To copy to clipboard, switch view to plain text mode 

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Populate a QTreeView from a string like it was a local directory

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    7

    Default Re: Populate a QTreeView from a string like it was a local directory

    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

Similar Threads

  1. Replies: 1
    Last Post: 23rd May 2011, 04:53
  2. Populate QTableWidget
    By ruben.rodrigues in forum Qt Programming
    Replies: 2
    Last Post: 3rd May 2011, 14:21
  3. PyQt Populate a QTableWidget
    By n3wcr4zy in forum Newbie
    Replies: 0
    Last Post: 2nd March 2011, 08:20
  4. Replies: 2
    Last Post: 4th April 2008, 12:00
  5. Populate a combobox from vector<string>
    By pkirk25 in forum Newbie
    Replies: 3
    Last Post: 15th November 2006, 23:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.