Skip to content

Commit eba4d03

Browse files
committed
Fix crash in attribute table when layer has no fields
1 parent dadc9d2 commit eba4d03

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/gui/attributetable/qgsattributetablefiltermodel.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
6969

7070
QVariant QgsAttributeTableFilterModel::data( const QModelIndex& index, int role ) const
7171
{
72-
if ( mColumnMapping.at( index.column() ) == -1 ) // actions
72+
if ( mapColumnToSource( index.column() ) == -1 ) // actions
7373
{
7474
if ( role == TypeRole )
7575
return ColumnTypeActionButton;
@@ -355,6 +355,14 @@ void QgsAttributeTableFilterModel::onColumnsChanged()
355355
setAttributeTableConfig( mConfig );
356356
}
357357

358+
int QgsAttributeTableFilterModel::mapColumnToSource( int column ) const
359+
{
360+
if ( mColumnMapping.isEmpty() )
361+
return 0;
362+
else
363+
return mColumnMapping.at( column );
364+
}
365+
358366
void QgsAttributeTableFilterModel::generateListOfVisibleFeatures()
359367
{
360368
if ( !layer() )
@@ -466,26 +474,29 @@ QModelIndex QgsAttributeTableFilterModel::mapToSource( const QModelIndex& proxyI
466474
if ( !proxyIndex.isValid() )
467475
return QModelIndex();
468476

469-
int sourceColumn = mColumnMapping.at( proxyIndex.column() );
477+
int sourceColumn = mapColumnToSource( proxyIndex.column() );
470478

471479
// For the action column there is no matching column in the source model: invalid
472480
if ( sourceColumn == -1 )
473481
return QModelIndex();
474482

475-
return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), mColumnMapping.at( proxyIndex.column() ), proxyIndex.parent() ) );
483+
return QSortFilterProxyModel::mapToSource( index( proxyIndex.row(), sourceColumn, proxyIndex.parent() ) );
476484
}
477485

478486
QModelIndex QgsAttributeTableFilterModel::mapFromSource( const QModelIndex& sourceIndex ) const
479487
{
480488
QModelIndex proxyIndex = QSortFilterProxyModel::mapFromSource( sourceIndex );
481489

482-
return index( proxyIndex.row(), mColumnMapping.indexOf( proxyIndex.column() ), proxyIndex.parent() );
490+
if ( proxyIndex.column() < 0 )
491+
return QModelIndex();
492+
493+
return index( proxyIndex.row(), mapColumnToSource( proxyIndex.column() ), proxyIndex.parent() );
483494
}
484495

485496
Qt::ItemFlags QgsAttributeTableFilterModel::flags( const QModelIndex& index ) const
486497
{
487498
// Handle the action column flags here, the master model doesn't know it
488-
if ( mColumnMapping.at( index.column() ) == -1 )
499+
if ( mapColumnToSource( index.column() ) == -1 )
489500
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
490501

491502
QModelIndex source_index = mapToSource( index );

src/gui/attributetable/qgsattributetablefiltermodel.h

+2
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
255255

256256
QgsAttributeTableConfig mConfig;
257257
QVector<int> mColumnMapping;
258+
int mapColumnToSource( int column ) const;
259+
258260
};
259261

260262
#endif

0 commit comments

Comments
 (0)