[Qt-interest] Model/view programming
Stephen Kelly
steveire at gmail.com
Thu Aug 25 16:24:12 CEST 2011
David Heremans wrote:
> 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.
I recommend you implement this with a tree model. Don't implement a table
and then try to use a proxy to turn it into a tree.
>
> Allow me to throw in a bunch of different questions here.
> Some of the issues I'm having:
>
> - If I rearrange the tree then the treeview will suddenly collapse open
> branches if the parent is removed. How can I prevent this?
You prevent that by keeping QPersistentModelIndexes up to date. If you don't
use a proxy this will likely become a non-issue.
> In the same
> category, if I add children to a tree, is there a way to tell the view
> to open the branch automatically?
subclass QTreeView and add a custom slot connected to the models
rowsInserted signal. Then use QTreeView API to expand it.
>
> - If I add rows then I have a line 'emit layoutAboutToBeChanged' but
> does this make the beginInsertRows/endInsertRows methods superflous?
If you use beginInsert/etc then layoutAboutToBeChanged is superflous.
>
> - 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?
Good question. I would say that it must return a valid index, otherwise bad
things might happen. For that reason, implementing a table-to-tree
QAbatractProxyModel is probably not the correct interface.
>
> - I tried the modeltest, is it normal that it tries to access the
> topIndex'(0,0,QModelIndex())' even if the rowcount of the model returns 0?
>
Which line in the modeltest? Which version of modeltest?
> - I came across a thing called PersistentIndexes, I'm not using this in
> my program. It is to say, at least not specifically programmed by me,
> are the view using this stuff. A good explication about what I have to
> do with these things would be nice since the documentation does not
> really tell you what (and how) to implement a new model with regard to
> those things.
>
I agree that the docs could be better.
Consider this:
You have a model with 5 rows.
- A
- B
- C
- D
- E
You do this:
QModelIndex idx0 = m_model->index(0, 0);
QModelIndex idx1 = m_model->index(1, 0);
At this point they are both valid. idx0 is A, idx1 is B.
Then you do this:
m_model->removeRow(0);
At this point idx0.row() == 0 and idx1.row() == 1. Is that correct?
idx0.data() now returns 'B' instead of 'A'. idx1.data() returns 'C' instead
of 'B'. Is that correct?
Now consider this:
You have the same model with 5 rows.
QPersistentModelIndex idx0 = m_model->index(0, 0);
QPersistentModelIndex idx1 = m_model->index(1, 0);
At this point they are both valid. idx0 is A, idx1 is B.
Then you do this:
m_model->removeRow(0);
The model tells the QPersistentModelIndex that the row was removed. idx0 is
now an invalid index (it was removed). idx1 now has row() == 0. idx1.data()
still returns 'B'.
So the difference is, QPersistentModelIndexes have their rows and columns
updated whenever the model changes. QModelIndexes don't.
Thanks,
Steve
More information about the Qt-interest-old
mailing list