Reputation: 5390
The documentation appears to say jack about this, and I've seen a bunch of ambiguous example code around StackOverflow and other places, so...
If I have a class A
that implements a QAbstractProxyModel
and a class B
that implements a QAbstractItemModel
and I call on an instance of A
the method setSourceModel(b)
where b
is an instance of B
, does that automatically take care of forwarding update signals such as modelReset
, rowsInserted
, etc.? Or must I manually connect all those?
Upvotes: 6
Views: 1488
Reputation: 31
If class A : public QAbstractProxyModel
, class B : public QAbstractItemModel
some signals are forwarding and updating well (dataChanged
, etc). But some (like rowsInserted
) you have to connect manually.
I use code like this:
...
#define COL_ID 0
void A::setSourceModel(QAbstractItemModel *newSourceModel) {
beginResetModel();
if (this->sourceModel()) { // disconnect sourceModel signals
...
}
...
QAbstractProxyModel::setSourceModel(newSourceModel);
if (this->sourceModel()) { // connect sourceModel signals
...
connect(this->sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(sourceRowsInserted(QModelIndex, int, int)));
...
}
return;
}
...
void A::sourceRowsInserted(const QModelIndex &parent, int first, int last) {
QModelIndex parentIndex = this->mapFromSource(parent);
QModelIndex sourceTopIndex = this->sourceModel()->index(first, COL_ID, parent);
QModelIndex sourceBottomIndex = this->sourceModel()->index(last, COL_ID, parent);
QModelIndex topIndex = this->mapFromSource(sourceTopIndex);
QModelIndex bottomIndex = this->mapFromSource(sourceBottomIndex);
beginInsertRows(parentIndex, topIndex.row(), bottomIndex.row());
endInsertRows();
return;
}
...
Upvotes: 1
Reputation: 2141
You are right, the documentation is very unhelpful regarding this. Looking at the source code of QAbstractProxyModel and comparing it to QSortFilterProxyModel in Qt 5.12, yields that QAbstractProxyModel does in no way handle forwarding of the dataChanged signal! You have to do it yourself! It's better to choose a more complex model like QSortFilterProxyModel or QIdentityProxyModel, which does this forwarding for your. But if you really can't get around it, then it could look like this:
/**
* Proxy model which only returns one data row of the underlying QAbstractItemModel
* except for the first column. Can be used to separate a model for QTreeView into
* the tree column and the data columns. This proxy returns the data columns.
* Can't use QSortFilterProxyModel because it does not allow for only showing one
* row if its parent is filtered out.
*/
class SingleRowProxy :
public QAbstractProxyModel
{
Q_OBJECT;
using BaseType = QAbstractProxyModel;
static constexpr auto FIRST_DATA_COLUMN = 1;
public:
SingleRowProxy( QAbstractItemModel* sourceModel,
int row,
const QModelIndex& parentIndex,
QObject* parentObject = nullptr ) :
BaseType( parentObject ),
m_sourceRow( row ),
m_sourceParent( parentIndex )
{
Q_ASSERT( sourceModel != nullptr );
setSourceModel( sourceModel );
}
void setSourceModel( QAbstractItemModel *newSourceModel ) override
{
if ( newSourceModel == sourceModel() ) {
return;
}
beginResetModel();
disconnect( newSourceModel, nullptr, this, nullptr );
BaseType::setSourceModel( newSourceModel );
connect( newSourceModel, &QAbstractItemModel::dataChanged,
this, &SingleRowProxy::sourceDataChanged );
connect( newSourceModel, &QAbstractItemModel::modelAboutToBeReset,
this, [this] () { beginResetModel(); } );
connect( newSourceModel, &QAbstractItemModel::modelReset,
this, [this] () { endResetModel(); } );
}
QModelIndex
mapFromSource( const QModelIndex& sourceIndex ) const override
{
if ( !sourceIndex.isValid() || ( sourceIndex.column() < FIRST_DATA_COLUMN ) ) {
return {};
}
return index( 0, sourceIndex.column() - FIRST_DATA_COLUMN, QModelIndex() );
}
QModelIndex
mapToSource( const QModelIndex& proxyIndex ) const override
{
if ( !proxyIndex.isValid() ) {
return {};
}
return sourceModel()->index( m_sourceRow,
proxyIndex.column() + FIRST_DATA_COLUMN,
m_sourceParent );
}
QVariant
data( const QModelIndex& index,
int role ) const override
{
return sourceModel()->data( mapToSource( index ), role );
}
int
rowCount( [[maybe_unused]] const QModelIndex& parent = QModelIndex() ) const override
{
return sourceModel()->hasIndex( m_sourceRow, FIRST_DATA_COLUMN, m_sourceParent ) ? 1 : 0;
}
int
columnCount( [[maybe_unused]] const QModelIndex& parent = QModelIndex() ) const override
{
return sourceModel()->columnCount( sourceModel()->index( m_sourceRow, 0, m_sourceParent ) );
}
QModelIndex
index( int row,
int column,
const QModelIndex& parent ) const override
{
if ( !hasIndex( row, column, parent ) ) {
return {};
}
return createIndex( row, column );
}
QModelIndex
parent( [[maybe_unused]] const QModelIndex& child ) const override
{
return {};
}
private slots:
/**
* QSortFilterProxyModel does it for us but QAbstractProxyModel does not!
* So we have to map the source indices and reemit the dataChanged signal. */
void
sourceDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
{
if ( !topLeft.isValid() ||
!bottomRight.isValid() ||
( topLeft.parent() != bottomRight.parent() ) )
{
return;
}
const auto& parent = topLeft.parent();
int minRow = std::numeric_limits<int>::max();
int maxRow = std::numeric_limits<int>::lowest();
int minCol = std::numeric_limits<int>::max();
int maxCol = std::numeric_limits<int>::lowest();
bool foundValidIndex = false;
for ( int sourceRow = topLeft.row(); sourceRow <= bottomRight.row(); ++sourceRow ) {
for ( int sourceColumn = topLeft.column(); sourceColumn <= bottomRight.column(); ++sourceColumn ) {
const auto index = mapFromSource( sourceModel()->index( sourceRow, sourceColumn, topLeft.parent() ) );
if ( !index.isValid() ) {
continue;
}
minRow = std::min( minRow, index.row() );
maxRow = std::max( maxRow, index.row() );
minCol = std::min( minCol, index.column() );
maxCol = std::max( maxCol, index.column() );
foundValidIndex = true;
}
}
if ( foundValidIndex ) {
emit dataChanged( index( minRow, minCol, parent ),
index( maxRow, maxCol, parent ),
roles );
}
}
private:
const int m_sourceRow;
const QModelIndex m_sourceParent;
};
You have to do index mapping because proxy indices are different from source model indices!
Note that this example is only very rudimentary and might not work for arbitrary mappings.
Note that for a complete proxy model you have to map and forward all signals. QSortFilterProxyModel reconnects these signals in setSourceModel:
Upvotes: 3
Reputation: 40512
From the documentation:
To subclass QAbstractProxyModel, you need to implement mapFromSource() and mapToSource(). The mapSelectionFromSource() and mapSelectionToSource() functions only need to be reimplemented if you need a behavior different from the default behavior.
There is no word about signals. And so it is in the documentation of mentioned methods. That means that you don't need to care about signals, they will be emitted automatically.
Upvotes: 1
Reputation: 2522
if the classes are like class A : public QAbstractProxyModel
and class B : public QAbstractItemModel
the signals and slots should get inherited as well. (except you want special behaviour for it.
if the "QAbstractClasses" are simple members of A
and B
you have to 'forward' them
Upvotes: 0