Results 1 to 14 of 14

Thread: fetchMore() not working on the top-level index in a QFileSystemModel

  1. #1
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default fetchMore() not working on the top-level index in a QFileSystemModel

    The directoryLoaded() signal is never emitted after calling fetchMore() on the top-level index (QModelIndex()) in a QFileSystemModel. CanFetchMore() returns true though...

    I use fetchMore() to be able to determine how many item are under a given index by using rowCount() after receiving directoryLoaded() signal. It works well for any item in my QFileSystemModel except for the top level one.

    Does anybody know why? How can I get this to work?

    Thanks

    In other words, how can I determine how many drives my file system has? (C:, D:, etc... I'm working on windows)

  2. #2
    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: fetchMore() not working on the top-level index in a QFileSystemModel

    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.


  3. #3
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: fetchMore() not working on the top-level index in a QFileSystemModel

    QDir::drives() gives me access to the number of drives, however it does it doesn't help for what I'm trying to do. I actually need to work indexes.
    I should not have put the last stentence in previous post... sorry.

    Here is the context.
    I'm not happy with the way QTreeView shows a tree sctructue. It shows it starting form the RootIndex's index chidren, not from RootIndex itself.

    Expected: (each "-" represents a level)
    -Root_item(1)
    -- item(1,1)
    --- item(1,1,1)
    --- item(1,1,2)
    --- item(1,1,3)
    -- item(1,2)
    --- item(1,2,1)
    --- item(1,2,2)
    -- item(1,3)

    Actual:
    -- item(1,1)
    --- item(1,1,1)
    --- item(1,1,2)
    --- item(1,1,3)
    -- item(1,2)
    --- item(1,2,1)
    --- item(1,2,2)
    -- item(1,3)

    I modified the behavior of setRootIndex() in my QTreeView, so it acctually sets the RootIndex one level higher, on the specified index's parent, and it hides all its chidren execpt the specified index using setRowHidden(). It works pretty well for any indexes in the QFileSystemModel except for the QFileSystemModel's topindex. (...if I want to display C:/ and its tree scructure for example).

    I tried QDir::drives() like you suggested. It is a work arround that helps me to get the number of items under the QFileSystemModel topindex instead of using QFileSystemModel:rowCount. I can then use a For Loop to apply QTreeView::setRowHidden() on each item but one like I do in the other cases. But it seems that QTreeView::setRowHidden() does not even work in that case. And I think this is because the data under the topindex is not loaded yet in the model at that point, which bring me to the original problem:
    The directoryLoaded() signal is never emitted after calling fetchMore() on the top-level index in a QFileSystemModel.

  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: fetchMore() not working on the top-level index in a QFileSystemModel

    To be honest I think you are going the wrong way. It seems easiest to obtain what you want by implementing a simple proxy model that would add the additional parent item to the model.
    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
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: fetchMore() not working on the top-level index in a QFileSystemModel

    Thank you for your suggestion. I liked my way, but I gave a try to yours. I never worked with proxy models before, so it is a good trainning anyway.

    The result is that I'm falling in the exact same situation than before. I managed to setup a "transparent" proxi that does not do anything between my QFileSystemModel and my QtreeView in the first place. That worked well.
    Now, when I try to insert a new index in the proxi to do the trick, I still have to deal with the fact that my QFileSystemModel source model populates incrementally. I cannot insert new index if the data of the trageted parent for the insertion is not loaded first in the QFileSystemModel. I have to use fetchMore() to do that, and since the loading process is asynchroneous, I have to "wait" until the corresponding directoryLoaded() signal is emitted to procced with the insertion. I end up with the same problem I had earlier. It works fine for any indexes execpt for the top-level index for which directoryLoaded() is never emitted after fetchMore() is querried...
    I'm back to the same issue...

  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: fetchMore() not working on the top-level index in a QFileSystemModel

    In that case maybe I simply fail to see what your problem is. I thought the problem was that you wanted to add an artificial index on top of the file system model. Now it seems you have problems with the file system model itself, at least if I understand you correctly.
    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
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: fetchMore() not working on the top-level index in a QFileSystemModel

    Both actually. My intend is to display the root index at the top of the tree view. To do that I tried two different approches:
    #1: Modify the setRootIndex() in my QTreeView to look one level higher and hide all the siblings of my original target index (my approch)
    #2: Use a proxy model to insert an artificial index right above the targeted index and then point at that artificial index as the root index.

    The two approches are valid but they fail to works with the top-level item. In both cases the nature of the QFileSysemModel requires the use of fetchMore() and directoryLoaded() signal, and
    fetchMore() does not work with the top-level item...

    Here is a little experiment to demonstrate the problem: (See attached the entire project)

    Qt Code:
    1. #include "SATT_MainWindow.h"
    2.  
    3. SATT_MainWindow::SATT_MainWindow(): QWidget()
    4. {
    5. setFixedSize(900,600);
    6.  
    7. //creates Run button
    8.  
    9. m_model = new QFileSystemModel(this);
    10. m_view = new QTreeView(this);
    11. m_view->setFixedSize(900,600);
    12. m_model->setRootPath(QDir::currentPath());
    13. m_view->setModel(m_model);
    14. QObject::connect(m_model, SIGNAL(directoryLoaded(QString)), this, SLOT(someSlot(QString)));
    15.  
    16. QModelIndex root = m_model->index("C:/");
    17. if (m_model->canFetchMore(root))
    18. {
    19. m_model->canFetchMore(root);
    20. }
    21. m_view->setRootIndex(root);
    22. }
    23.  
    24. void SATT_MainWindow::someSlot(const QString &path)
    25. {
    26. int rowCountTest = (m_model->rowCount(m_model->index(path)));
    27. }
    To copy to clipboard, switch view to plain text mode 

    Put a break point at line 19 and at line 26.
    If you run it like this the execution will first stop at line 19, then once the the QFileSystemModel has finished working in the background, the directoryLoaded signal is emitted. It is caught by the slot and the execution stops at line 26. You can excute the step 26 and you can verify that rowCount() sends the right number of element under C:/ (meaning that the data the index is loaded).
    Now if you replace line 16 by:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    QModelIndex() beeing the top-item for any tree structue, It won't work. The execution will stop at line 19, but will never stop at line 25 FOR THAT FETCHMORE() CALL...
    Attached Files Attached Files

  8. #8
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: fetchMore() not working on the top-level index in a QFileSystemModel

    Sorry, I meant: m_model->fetchMore(root); on line 19 of course

  9. #9
    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: fetchMore() not working on the top-level index in a QFileSystemModel

    And how did you implement the proxy model for your model?
    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.


  10. #10
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: fetchMore() not working on the top-level index in a QFileSystemModel

    I have implemented the proxi model in the view itself, because only that view needs that trick with an artificial index to display the rootindex.
    I create the proxi model when I call setModel(). SetModel() is called with the source model.

    Qt Code:
    1. void FileSysSelectView::setModel(QAbstractItemModel * model)
    2. {
    3.  
    4. m_sourceModel = static_cast<FileSysSelectModel *> (model);
    5. m_proxiModel = new QSortFilterProxyModel();
    6. m_proxiModel->setSourceModel(m_sourceModel);
    7.  
    8. QTreeView::setModel(m_proxiModel);
    9.  
    10. // Limit the view to the File name Column (1st Column)
    11. for (int i(1); i < m_proxiModel->columnCount(QModelIndex::QModelIndex()); i++) this->setColumnHidden(i,true);
    12. }
    To copy to clipboard, switch view to plain text mode 
    At that point everything works well, my proxi model sits bettween my view and the source model, and it acts transparently.
    I try to insert the artificial index when I call setRootIndex().

    For exemple, if I want to show what is under C:/, I have to do the folowing:

    before:
    - root
    --C:\
    ---Program Files
    ---Windows
    ---Users
    --D:\
    --X:\

    after:
    - root
    -- artififical index
    ---C:\
    ----Program Files
    ----Windows
    ---Users
    --D:\
    --X:\
    Qt Code:
    1. void FileSysSelectView::setRootIndex(const QModelIndex & index)
    2. {
    3.  
    4. // insert a new index under the specified root index.
    5. QModelIndex idxParent = m_proxiModel->mapFromSource(index.parent());
    6. m_proxiModel->insertRow(0, idxParent);
    7.  
    8. // move the the root index under the new index
    9. QModelIndex idxNew = m_proxiModel->index(0,0, idxParent);
    10. QModelIndex idx = m_proxiModel->mapFromSource(index);
    11. int idxPosition = idx.row();
    12. m_proxiModel->moveRow(idxParent, idxPosition, idxNew, 0);
    13. QTreeView::setRootIndex(idx);
    14. }
    To copy to clipboard, switch view to plain text mode 
    Actually int idxPosition = idx.row() at line 11 doesn't work for the reasons I mentioned earlier in the thread. The underlying QFileSystemModel needs to be populated before idx.row() is called. That means I have to call fetchMore() and check for directroyLoaded() signal in that situation too...

  11. #11
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: fetchMore() not working on the top-level index in a QFileSystemModel

    Also, I have found another old thread from a different forum that said:

    "Regarding folder sorting on Windows: I think I got it. The damn model only sorts items below its "root path()" as the statrting point which is "/" on Linux /UNIX / Mac Os X, but on windows it's the system driver's root directory. Effectively, only "c:/" and it's sub-folders were sorted...
    Now I'm using "QFileSystemModel::myComputer" as the model's root and as a result everything is sorted as expected."

    http://forums.bannister.org/ubbthrea...68709&page=289

    That might be related to my issue. I don't know how that person managed to use "QFileSystemModel::myComputer" as the model's root, though... it does not retrun a QModelIndex.

  12. #12
    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: fetchMore() not working on the top-level index in a QFileSystemModel

    A sort-filter proxy model can't do what you require. You have to implement your own proxy model derived either from QAbstractProxyModel or from QIdentityProxyModel.
    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.


  13. #13
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: fetchMore() not working on the top-level index in a QFileSystemModel

    QIdentityProxyModel is what I tried in the first place. It didn't work either. I'm not familiar whith proxi models. why do I need my own proxi? Could you give my some guidance or some useful links...
    I search the internet without any success for that...

  14. #14
    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: fetchMore() not working on the top-level index in a QFileSystemModel

    Quote Originally Posted by Guett_31 View Post
    QIdentityProxyModel is what I tried in the first place. It didn't work either.
    It won't "work" because it is a transparent proxy. You need to subclass it and implement the functionality you need.

    I'm not familiar whith proxi models. why do I need my own proxi?
    Because you want to add an artificial item to an existing model without changing the original model itself.
    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.


Similar Threads

  1. Replies: 3
    Last Post: 26th September 2012, 21:21
  2. QComboBox::insertSeparator(int index) is not working
    By osiris81 in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2011, 19:43
  3. QFileSystemModel::remove not removing index from model
    By revorgm in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 10:24
  4. Beginner level program not working
    By Nishant in forum Newbie
    Replies: 9
    Last Post: 11th June 2010, 15:46
  5. Crash while checking the index is of particular level of a tree
    By kapil sharma in forum Qt Programming
    Replies: 1
    Last Post: 27th November 2008, 08:21

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.