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.
{
m_sourceModel = static_cast<FileSysSelectModel *> (model);
m_proxiModel->setSourceModel(m_sourceModel);
// Limit the view to the File name Column (1st Column)
}
void FileSysSelectView::setModel(QAbstractItemModel * model)
{
m_sourceModel = static_cast<FileSysSelectModel *> (model);
m_proxiModel = new QSortFilterProxyModel();
m_proxiModel->setSourceModel(m_sourceModel);
QTreeView::setModel(m_proxiModel);
// Limit the view to the File name Column (1st Column)
for (int i(1); i < m_proxiModel->columnCount(QModelIndex::QModelIndex()); i++) this->setColumnHidden(i,true);
}
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:\
void FileSysSelectView
::setRootIndex(const QModelIndex & index
) {
// insert a new index under the specified root index.
QModelIndex idxParent
= m_proxiModel
->mapFromSource
(index.
parent());
m_proxiModel->insertRow(0, idxParent);
// move the the root index under the new index
QModelIndex idxNew
= m_proxiModel
->index
(0,
0, idxParent
);
int idxPosition = idx.row();
m_proxiModel->moveRow(idxParent, idxPosition, idxNew, 0);
}
void FileSysSelectView::setRootIndex(const QModelIndex & index)
{
// insert a new index under the specified root index.
QModelIndex idxParent = m_proxiModel->mapFromSource(index.parent());
m_proxiModel->insertRow(0, idxParent);
// move the the root index under the new index
QModelIndex idxNew = m_proxiModel->index(0,0, idxParent);
QModelIndex idx = m_proxiModel->mapFromSource(index);
int idxPosition = idx.row();
m_proxiModel->moveRow(idxParent, idxPosition, idxNew, 0);
QTreeView::setRootIndex(idx);
}
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...
Bookmarks