[Interest] Why are QAbstractProxyModel::mapToSource and mapFromSource public?

Jan Kundr√°t jkt at flaska.net
Wed Dec 11 18:30:36 CET 2013


On Wednesday, 11 December 2013 17:36:36 CEST, Philipp Kursawe wrote:
> In code that would be a loop over pX->sourceModel while the sourceModel 
is
> a QAbstractProxyModel.

That's very similar to what I do in my code, too -- just loop as long as 
the qobject_cast<QAbstractProxyModel*>(translated.model()) is not a 
nullptr. It's also he right thing to do, IMHO. Each proxy model only cares 
about its own business. The fact that its source model also happens to be a 
proxy is not important from the first proxy's point of view.

> I just wonder why the index mapping was made public API in the first 
place?

For a real-world example, I've checked Trojita's source code and it indeed 
seems that most of calls to mapFromSource are done only in context of the 
proxy model itself.

However, the situation is very different with mapToSource. Some operations 
which the code performs cannot be easily expressed via the traditional MVC 
API, so there are custom "modifier" methods which accept a QModelIndex 
instance. This code asserts to make sure that if a valid index is passed, 
it must belong to this model. This in turn means that something has to 
convert the proxy indexes all the way back to the original index. Given 
that this code very likely runs in the scope of the GUI, it makes sense to 
just call some utility function which then needs the mapToSource, and 
therefore that cannot be a private method.

Hope this helps,
Jan

-- 
Trojit√°, a fast Qt IMAP e-mail client -- http://trojita.flaska.net/



More information about the Interest mailing list