[Interest] Issue with QSortFilterProxyModel and Delegate
Jan Kundrát
jkt at flaska.net
Thu May 10 14:54:19 CEST 2012
On 05/10/12 13:52, André Somers wrote:
> That seems completely logical to me. As you found out, you need to check
> any pointer you get back to see if it is not 0 before you dereference
> it. In your case, your issue is that you're mixing indexes from your
> proxy model and your source model. Don't do that. They can be mapped to
> each other, but only by the proxy model itself.
To expand a bit on this answer -- the error you (the original poster)
have made is looking at an index an assuming that it comes from a
QStandardItemModel. That's a wrong assumption which doesn't hold in your
example.
Generally speaking, I'd say it's preferable to use QModelIndex::data
with custom roles in preference to doing manual casting of the
underlying model (as Andre correctly suggested). If you want to use
explicit casting for some reason (maybe your data cannot really go
through the QVariant marshaling, or you need to use your model's methods
directly, or...), a correct way is to use something safer than the
C-style casts, ie. something like this:
Q_ASSERT(index.isValid());
YourModelClass *model =
qobject_cast<YourModelClass*>(const_cast<QAbstractItemModel*>(index.model()));
Q_ASSERT(model);
// now do something with your model here
As usual, the Q_ASSERT blocks might be replaced with an appropriate
error handling code -- if the second one failed, you know that the rest
of the code passed you another model -- either through a proxy model
(which happened in your case), or maybe because you've changed your code
and the GUI gets passed an index from an address book model in context
of rendering a calendar, etc.
Again, I'd recommend sticking with using custom model roles for passing
item-specific data around your classes; Andre is spot-on about
needlessly tying your code into one specific model implementation. The
Qt's interview classes encourage one to separate the model from the
rest, so one shall probably try to use this separation.
With kind regards,
Jan
--
Trojita, a fast e-mail client -- http://trojita.flaska.net/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120510/7dadff81/attachment.sig>
More information about the Interest
mailing list