This gets more and more messy! I've implemented this to swap column 0 with the branch column in order to always have the branches in the far left column, even after user column rearrange.
However after the change in mapFramSource and mapToSource no data is displayed in the tree.
QModelIndex MyProxyModel
::mapFromSource(const QModelIndex
& sourceIndex
) const {
if (sourceIndex.isValid())
{
int key
= sourceModel
()->data
(sourceModel
()->index
(sourceIndex.
row(), m_sourcekeycolumn,
QModelIndex()) ).
toInt() ;
Record *rec = internaltable->m_map.value(key);
int row = 0;
if(rec->parent)
row = rec->parent->children.indexOf(rec);
const int brcol = getBranchColumn();
if( sourceIndex.column() == brcol ) {
QModelIndex changed
= sourceIndex.
sibling(row,
0);
//new return changed;
}
if( sourceIndex.column() == 0 ){
QModelIndex changed
= sourceIndex.
sibling(row, brcol
);
//new return changed;
}
return createIndex(row,sourceIndex.column(),rec) ;
}
}
//
QModelIndex MyProxyModel
::mapToSource(const QModelIndex
& proxyIndex
) const {
if(proxyIndex.isValid())
{
Record *rec = recordFromIndex(proxyIndex);
const int brcol = getBranchColumn();
if(rec)
{
if( proxyIndex.column() == brcol ) {
QModelIndex changed
= proxyIndex.
sibling(proxyIndex.
row(),
0);
//new return changed;
}
if( proxyIndex.column() == 0 ){
QModelIndex changed
= proxyIndex.
sibling(proxyIndex.
row(), brcol
);
//new return changed;
}
}
}
}
QModelIndex MyProxyModel::mapFromSource(const QModelIndex& sourceIndex) const
{
if (sourceIndex.isValid())
{
int key = sourceModel()->data(sourceModel()->index(sourceIndex.row(), m_sourcekeycolumn,QModelIndex()) ).toInt() ;
Record *rec = internaltable->m_map.value(key);
int row = 0;
if(rec->parent)
row = rec->parent->children.indexOf(rec);
const int brcol = getBranchColumn();
if( sourceIndex.column() == brcol ) {
QModelIndex changed = sourceIndex.sibling(row, 0); //new
return changed;
}
if( sourceIndex.column() == 0 ){
QModelIndex changed = sourceIndex.sibling(row, brcol ); //new
return changed;
}
return createIndex(row,sourceIndex.column(),rec) ;
}
return QModelIndex();
}
//
QModelIndex MyProxyModel::mapToSource(const QModelIndex& proxyIndex) const
{
if(proxyIndex.isValid())
{
Record *rec = recordFromIndex(proxyIndex);
const int brcol = getBranchColumn();
if(rec)
{
if( proxyIndex.column() == brcol ) {
QModelIndex changed = proxyIndex.sibling(proxyIndex.row(), 0); //new
return changed;
}
if( proxyIndex.column() == 0 ){
QModelIndex changed = proxyIndex.sibling(proxyIndex.row(), brcol ); //new
return changed;
}
}
}
return QModelIndex() ;
}
To copy to clipboard, switch view to plain text mode
My model hieracy is QStandarItemModel to QAbstractProxyModel to QSortFilterProxyModel so the source model for the treeview is QSortFilterProxyModel. Why can't I squeeze more functionality out of the two extra proxy models to archive the column swap? I had really hoped for more help from one of the proxies!
What an I doing wrong?
Bookmarks