Results 1 to 1 of 1

Thread: problem implementing proxy models in series

  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 problem implementing proxy models in series

    Here is what I’m trying to achieve:
    I want to display sub parts of the File System in different windows from which I can select files.
    I want that the selection automatically applies to all the files and all the sub-folders under the selection and I also want that each view displays the tree structure this way:
    -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)

    In order to do those in an efficient way, I have decided to use proxy models so I can use only one instance of QFileSystemModel for all my views.
    I use two proxy layers, one for the selection feature and anoter the "single root tree" display:
    QFileSystemModel->ItemSelectionProxyModel->DisplayRootItemProxyModel->QTreeView. If I set my proxies that way everything works perfectly.

    The problem occurs when I set my proxies like this:QFileSystemModel->DisplayRootItemProxyModel->ItemSelectionProxyModel->QTreeView. In that case the application fails to automatically select a few items (always the same ones) when I select a folder with a lot of files and subfolders.

    I designed my two proxies from a IdentityFileSystemProxyModel base class, which is sub-classed from QIdentityProxyModel.
    Here is code of the proxy base class, IdentityFileSystemProxyModel:
    Qt Code:
    1. {
    2. class IdentityFileSystemProxyModel : public QIdentityProxyModel
    3. Q_OBJECT
    4. protected:
    5.  
    6. QAbstractItemModel * m_model;
    7. public:
    8.  
    9. IdentityFileSystemProxyModel(QObject *parent = 0);
    10.  
    11. void setSourceModel(QAbstractItemModel * sourceModel);
    12. virtual int columnCount(const QModelIndex & parent) const;
    13. virtual QString filePath(const QModelIndex & index) const;
    14.  
    15. signals:
    16. void directoryLoaded(QString path);
    17.  
    18. public slots:
    19. void directoryLoadedSlot(QString path);
    20. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "IdentityFileSystemProxyModel.h"
    2.  
    3. IdentityFileSystemProxyModel::IdentityFileSystemProxyModel(QObject *parent) : QIdentityProxyModel(parent)
    4. {
    5. }
    6.  
    7. void IdentityFileSystemProxyModel::setSourceModel(QAbstractItemModel * sourceModel)
    8. {
    9. m_model = sourceModel;
    10. QIdentityProxyModel::setSourceModel(m_model);
    11. QObject::connect(m_model,SIGNAL(directoryLoaded(QString)),this,SLOT(directoryLoadedSlot(QString)));
    12. }
    13.  
    14. int IdentityFileSystemProxyModel::columnCount(const QModelIndex & parent) const
    15. {
    16. return 1;
    17. }
    18.  
    19. QString IdentityFileSystemProxyModel::filePath(const QModelIndex & index) const
    20. {
    21. return data(index, Qt::UserRole + 1).toString();
    22. }
    23.  
    24. void IdentityFileSystemProxyModel::directoryLoadedSlot(QString path)
    25. {
    26. emit directoryLoaded(path);
    27. }
    To copy to clipboard, switch view to plain text mode 
    Here is how the QFileSystemModel , the two proxies and the view are connected for QFileSystemModel->DisplayRootItem->ItemSelectionProxyModel->QTreeView:
    Qt Code:
    1. void FileSysSelectView::setRootIndex(const QModelIndex & index)
    2. {
    3. //from sourceModel to rootItemModel
    4. QModelIndex proxIdx = m_rootModel->mapFromSource(index);
    5.  
    6. m_rootModel->setHomeIndex(proxIdx);
    7. proxIdx = m_rootModel->getHomeIndex();
    8.  
    9. //from rootItemModel to selectModel
    10. proxIdx = m_selectModel->mapFromSource(proxIdx);
    11.  
    12. QTreeView::setRootIndex(proxIdx);
    13. }
    14.  
    15. void FileSysSelectView::setModel(QAbstractItemModel * model)
    16. {
    17. m_sourceModel = static_cast<QFileSystemModel *> (model);
    18. m_rootModel = new RootItemProxyModel();
    19. m_selectModel = new SelectItemProxyModel();
    20.  
    21. m_rootModel->setSourceModel(m_sourceModel);
    22. m_selectModel->setSourceModel(m_rootModel);
    23.  
    24. QTreeView::setModel(m_selectModel);
    25. QObject::connect(m_selectModel,
    26. SIGNAL(dataChanged(QModelIndex, QModelIndex)),
    27. this,
    28. SLOT(ExpandItems(QModelIndex, QModelIndex)),
    29. Qt::DirectConnection);
    30. }
    To copy to clipboard, switch view to plain text mode 
    I think the problem comes from the IdentityFileSystemProxyModel base class:
    If I do QFileSystemModel->ItemSelectionProxyModel->QTreeView , I have no selection problem.
    If I do QFileSystemModel->IdentityFileSystemProxyModel->ItemSelectionProxyModel->QTreeView, I have the same selection problem I mentioned earlier.
    I have been unsuccessful to figure out what was wrong so far...

    Attached is a compilable project that shows the problem.

    Thanks in advance!
    Attached Files Attached Files

Similar Threads

  1. Use of signals and slots with proxy models.
    By Guett_31 in forum Qt Programming
    Replies: 3
    Last Post: 26th April 2013, 22:49
  2. Replies: 0
    Last Post: 16th October 2011, 02:27
  3. Replies: 0
    Last Post: 1st July 2010, 21:55
  4. Proxy models and slection models
    By amunitz in forum The Model-View Framework
    Replies: 1
    Last Post: 23rd September 2008, 14:35
  5. Proxy problem
    By MrShahi in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2008, 16:11

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.