Results 1 to 9 of 9

Thread: Simple QFileSystemModel and TreeView Example

  1. #1
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Simple QFileSystemModel and TreeView Example

    Hi All!

    First post here and I am certainly a QT newbie.

    In my quest to learn the QT5 API & Widgets, I thought I would make a simple file explorer to allow a user to navigate a directory structure. I was pleasantly amazed at how quickly I could get this working in QT with the following code:

    Qt Code:
    1. void MainWindow::initTreeView()
    2. {
    3. dirModel = new QFileSystemModel(this);
    4. dirModel->setRootPath("");
    5. dirModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
    6. ui->treeView->setModel(dirModel);
    7. }
    To copy to clipboard, switch view to plain text mode 

    This works great at first, however as I navigate the ui->treeView I observed the following:

    * Expanding a node with a large number of subdirectories (say over 10,000) causes the UI to become unresponsive for ~8-10 seconds. On my system (Windows 7), for instance C:\Windows\winsxs can easily have 10k+ subdirectories. According to the official QT documentation:

    Unlike QDirModel(obsolete), QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.
    * After expanding a large directory/node then collapsing this node as noted above, expanding other nodes (even nodes with only a few directories) becomes sluggish with a 2-3 second delay before the UI becomes responsive again.

    I've also noticed that Windows Explorer can expand and navigate these structures instantly (with no detectable delay) so I'm most likely not doing something right here. Am I missing some optimization setting I should be turning on or am I expecting too much from QFileSystemModel?

    Thanks in advance for any help!

    - Zep

  2. #2
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Simple QFileSystemModel and TreeView Example

    Well,

    No suggestions from the guru's here yet and QFileSystemModel would seem to me to be something that everyone that knows QT would have experience with........In any case, I'm still fighting with the unresponsive UI problem I mentioned above however I (quite by accident) ran into a partial solution for the issue:
    After expanding a large directory/node then collapsing this node as noted above, expanding other nodes (even nodes with only a few directories) becomes sluggish with a 2-3 second delay before the UI becomes responsive again.
    By replacing:
    Qt Code:
    1. dirModel->setRootPath("");
    To copy to clipboard, switch view to plain text mode 
    with:
    Qt Code:
    1. dirModel->setRootPath("/");
    To copy to clipboard, switch view to plain text mode 

    I have no clue why, but that minor change seems to have resolved the performance problem. But now the items in the model for my treeView are unsorted.......perhaps the implementation of the sorting code in QFileSystemModel needs some work.

    Anyway, I'll add to this post if I find additional solutions as I'm still struggling with keeping my UI responsive. Hopefully those learning QT, as I am, will find this helpful.

    Thanks,

    Zep

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Simple QFileSystemModel and TreeView Example

    * Expanding a node with a large number of subdirectories (say over 10,000) causes the UI to become unresponsive for ~8-10 seconds. On my system (Windows 7), for instance C:\Windows\winsxs can easily have 10k+ subdirectories.
    I sure something is missing.

    * After expanding a large directory/node then collapsing this node as noted above, expanding other nodes (even nodes with only a few directories) becomes sluggish with a 2-3 second delay before the UI becomes responsive again.
    I sure something is missing.

    I've also noticed that Windows Explorer can expand and navigate these structures instantly (with no detectable delay) so I'm most likely not doing something right here. Am I missing some optimization setting I should be turning on or am I expecting too much from QFileSystemModel?
    It is very obvious not to wonder why the native applications can perform better. Don't expect a better performance (at-least in this case)

    I have no clue why, but that minor change seems to have resolved the performance problem.
    There is clearly a difference between "" (nothing) and "/" (root directory)

    But now the items in the model for my treeView are unsorted.......perhaps the implementation of the sorting code in QFileSystemModel needs some work.
    Enable sorting on the QTreeView

    If this is not solved, I will look at this some time next week
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Simple QFileSystemModel and TreeView Example

    Santosh,

    I sure something is missing.
    What exactly would be missing?

    It is very obvious not to wonder why the native applications can perform better. Don't expect a better performance (at-least in this case)
    C++ QT Applications are native applications. If they are not then I've been misinformed.

    There is clearly a difference between "" (nothing) and "/" (root directory)
    Clearly, do you know what the difference is? If I missed it in the documentation then my mistake.

    Enable sorting on the QTreeView
    I'm on that path, as long as performance doesn't suffer for large data sets.

    If this is not solved, I will look at this some time next week
    I would prefer a pointer to documentation or a hint rather than solving this problem for me. I simply thought I would ask here in this "friendly" forum for newbies to see if this has already been solved by other friendly QT users. No point in re-inventing the wheel.

    Thanks,

    Zep

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Simple QFileSystemModel and TreeView Example

    C++ QT Applications are native applications. If they are not then I've been misinformed.
    Qt provides a consistent interface across the platforms, it uses the native APIs, using a wrapping/mapping mechanism (allowed as per C++, or interpreted by moc). Yes, you are right in making a statement that Qt Apps are native applications (in the way they execute), but they still differ from the application built on native technologies (frameworks), as for a simple reason that wrapping functionality brings in things that a cross-platform behavior needs.

    Clearly, do you know what the difference is? If I missed it in the documentation then my mistake.
    I can clearly see the difference between ""(nothing) and "/"(root). Please don't expect the documentation to mention all the invalid parameters that can be supplied.

    I'm on that path, as long as performance doesn't suffer for large data sets.
    For sure there will be a performance degrade for large data sets. To avoid this one will need to implement a special mechanism to sort, like proxy models, sorting in a worker thread etc.

    I would prefer a pointer to documentation or a hint rather than solving this problem for me. I simply thought I would ask here in this "friendly" forum for newbies to see if this has already been solved by other friendly QT users. No point in re-inventing the wheel.
    First things first, sure this is a friendly forum (if I may speak on behalf). I said so as a friendly gesture to see at the post (if not solved). Sure I don't have time to re-invent the wheel
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Jan 2013
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Simple QFileSystemModel and TreeView Example

    Santosh,

    Thank you for your reply and your time spent helping me to understand this.

    Qt provides a consistent interface across the platforms, it uses the native APIs, using a wrapping/mapping mechanism (allowed as per C++, or interpreted by moc). Yes, you are right in making a statement that Qt Apps are native applications (in the way they execute), but they still differ from the application built on native technologies (frameworks), as for a simple reason that wrapping functionality brings in things that a cross-platform behavior needs.
    Yes, I understand the need for a layer to enable a cross platform framework such as QT however an 8-10 second delay while my entire UI becomes unresponsive is not acceptable to me nor my end users. QT is such a well written API that I must assume I am doing something wrong, hence my original post. The documentation for QFileSystemModel clearly states:

    Unlike QDirModel(obsolete), QFileSystemModel uses a separate thread to populate itself so it will not cause the main thread to hang as the file system is being queried. Calls to rowCount() will return 0 until the model populates a directory.
    If this is the case, why is my UI unresponsive? Keep in mind my UI consists of a single QTreeView Widget.

    I can clearly see the difference between ""(nothing) and "/"(root). Please don't expect the documentation to mention all the invalid parameters that can be supplied.
    I agree with that. However, a well designed API will guard against invalid parameters. I see QT as a well designed API, hence my thought that I was doing something wrong.

    Also, I don't want to derail this thread but in the few weeks I've been lurking here and reading posts I've noticed some minor hostility toward those asking questions or trying to learn QT. As I'm evaluating QT for a commercial application, the community is very important to us, especially for an open source project like QT. Are there other sites you would recommend we visit to get a less narrow minded view of the QT community?

    Thanks,

    Zep

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Simple QFileSystemModel and TreeView Example

    I know we have deviated a bit from the thread topic, but it could be informative to others forum users

    Again first thing is that we are dealing with Qt (cute) not QT.

    If this is the case, why is my UI unresponsive? Keep in mind my UI consists of a single QTreeView Widget.
    QTreeView depends on QAbstractItemModel. If the model does not provide data in time, or just goes for a hike for 8 to 10 seconds (just kidding), QTreeView has nothing else to do but to wait for whatever time it takes the model to return the data.

    I agree with that. However, a well designed API will guard against invalid parameters. I see QT as a well designed API, hence my thought that I was doing something wrong.
    I will say the well designed API did not crash on invalid parameter, it did sustain itself and the software reading the output has to appreciate that

    Also, I don't want to derail this thread but in the few weeks I've been lurking here and reading posts I've noticed some minor hostility toward those asking questions or trying to learn QT.
    Well it all depends on how people take it. As far as I understand all such post(s) are to motivate the users to think/research/learn from the information, questions, discussions and off-course talks (like this post).

    As I'm evaluating QT for a commercial application, the community is very important to us, especially for an open source project like QT.
    Qt Commercial and Qt Project(open source) are very different when it comes to licensing. Please don't get confused between them.
    Are there other sites you would recommend we visit to get a less narrow minded view of the QT community?
    Obviously the project site http://qt-project.org/
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #8
    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: Simple QFileSystemModel and TreeView Example

    I don't really want to reply to all that was written here so I'll be brief. To solve the issue I suggest the OP takes a look at the source code of QFileSystemModel to see why "" and "/" differ and why his GUI gets stalled for 8-10 seconds. We have no access to his code so the only thing we can do is to look into QFileSystemModel ourselves which I'm assuming the OP is capable of doing on his own.

    Edit: Here is the source code: http://qt.gitorious.org/qt/qtbase/bl...ystemmodel.cpp
    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.


  9. #9
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: Simple QFileSystemModel and TreeView Example

    Quote Originally Posted by Santosh Reddy View Post
    I sure something is missing.


    I sure something is missing.


    It is very obvious not to wonder why the native applications can perform better. Don't expect a better performance (at-least in this case)


    There is clearly a difference between "" (nothing) and "/" (root directory)


    Enable sorting on the QTreeView

    If this is not solved, I will look at this some time next week
    Santosh, you're an absolute [*****]. Thanks for being "that guy" in the forums who takes pleasure in being a [****].
    Last edited by wysota; 14th April 2013 at 23:33. Reason: Censored

Similar Threads

  1. Replies: 10
    Last Post: 25th November 2010, 12:33
  2. Help with QFileSystemModel
    By TheShow in forum Qt Programming
    Replies: 4
    Last Post: 5th January 2010, 21:11
  3. Notifying QFileSystemModel about changes
    By squidge in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2009, 00:24
  4. QDirModel or QFileSystemModel?
    By ricardo in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2009, 18:10
  5. QFileSystemModel has no sorting
    By mpi in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2009, 09:14

Tags for this Thread

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.