[PyQt] Refresh a treeWidget but not it's expand/collapse
Joel B. Mohler
joel at kiwistrawberry.us
Mon Dec 5 12:57:45 GMT 2011
On 12/4/2011 12:42 PM, Christopher Evans wrote:
> What's the best way to refresh/rebuild a treeWidget without blowing
> away the user's depth traversal and framing?
>
> Anyone have a small fn for this? I guess you query the state of all
> leaves/branches of the treeWidget then reapply it?
In qtalchemy, I save selection and expansion state in the
"modelAboutToBeReset" signal and restore the state in "modelReset". I
save a list of the expanded nodes. You can see the source for yourself
at
https://bitbucket.org/jbmohler/qtalchemy/src/2a6a6c014c1f/qtalchemy/widgets/tree.py
-- the main logic is in the saveRowState and loadRowState at line 115
and 129 respectively. The state is saved in the RowState class at the
bottom of that source file. It's very short & basic, but works for my
applications.
I suppose it's also crucial to understand that I store the object id
(obtained by objectConverter which takes an object and returns the
primary key) which is most commonly a table primary key. I imagine it
might sometimes make sense to store the model index which would be the
integer index, but then you'd need to deal with the fact that you might
be inserting more records and therefore your integer indices change on
reload. Obviously, I'm thinking fairly strictly in terms of a database
driven application here.
If you have cells other than column zero expanded it will fail (I
hard-code 0 in the columnIndices helper function), but I haven't yet
been able to fully grok a sensible usage for the Qt hierarchical model
notions beyond expanding only on column 0. More exotic hierarchies seem
discouraged anyhow.
Joel
More information about the PyQt
mailing list