[Qt-interest] Model/view programming

Bo Thorsen bo at fioniasoftware.dk
Thu Aug 25 11:34:15 CEST 2011


Den 25-08-2011 11:16, David Heremans skrev:
> Hello,
>
> I'm trying to create my own datamodel and proxymodel but I'm having some
> serious trouble getting it all to 'play nice', the thing keeps acting
> weird and segfaulting.
> Does anybody knows some good in-dept tutorials/documentation about this ?
>
> I'm trying to create a proxy that re-arranges a 'ps -ef'-like table into
> a tree view depending on PID and PPID. Only the PPID and PID fields in
> the table can change so the proxy will have to rearrange them on-the fly
> as data changes.
>[...]
> - If I have a QModelIndex in my proxy that I inserted that has no
> equivalent in the sourceModel. What value does the MaptoSource needs to
> return?

This bit shows you can't use a proxy. They are for sorting and filtering 
only.

What you need to do is create a tree model from scratch. You can either 
drop the linear model, or you can listen to the add/remove signals in 
your tree model and modify the tree based on those.

The problem with recreating the tree every time is that you will loose 
current position and selection. This is normally annoying for users. But 
if that's not the case for your application, just recreate the tree from 
scratch and call reset(). You can work around this problem by catching 
the reset in the view, saving the current PID selection and set the 
right model index after the reset again.

You might not have a choice in this, though. The tree model or view 
doesn't understand if an index moves to be a child or parent in a 
different level of the tree. This case can only be handled by the PID 
matching.

Bo Thorsen,
Fionia Software.

-- 

Expert Qt and C++ developer for hire
Contact me if you need expert Qt help
http://www.fioniasoftware.dk



More information about the Qt-interest-old mailing list