[Development] QtWidgets Item / Model / View: tree model examples

Carl Schwan carl at carlschwan.eu
Thu Nov 23 13:18:10 CET 2023


On Tuesday, 21 November 2023 16:31:02 GMT+1 Laszlo Papp wrote:
> Hi,
>
> .......
> 
> Long time ago, I based my projects on these examples, inventing (copying
> and pasting) these tree items.
> 
> I wonder whether these examples could instead propagate the use of:
> 
> 1. QTreeWidgetItem?
> 2. QStandardItem?
> 
> It seems that e.g. the QTreeWidgetItem is nearly the same as the Tree Item
> invented in those examples. So, why reinvent?
> 
> Do you think that the tree item still has a good use case to exist in those
> examples?
> 
> If yes, what is it?
> 
> If not, could we start propagating QTreeWidgetItem or QStandardItem in
> those examples instead to avoid reinventing?

I also do that quite often for static tree models which is the reason why I 
recently ported the examples to use std::unique_ptr for memory management as 
it was one of the main modification I did after copying the example.

The other two modifications I always do is to change the type contained in the 
TreeItem from QVariantMap to something else (struct or list of arguments) and
change the data and roleNames method accordingly.

See for example https://invent.kde.org/graphics/arianna/-/blob/master/src/
tableofcontentmodel.cpp?ref_type=heads#L9

I also did wonder if it would make sense to have this as a standalone 
component provided by Qt (ideally as a templated class as I'm not a big fan of 
the QVariant approach of QStandardItem), but I don't think it is suitable for 
more complex use cases than the very basic tree.

But the example is quite helpful to understand how to build a more complex 
tree model and it can also be used as a base. A tree model (and a list model) 
are used to abstract the underlying data without having to convert the data 
beforhand in another data structure (e.g QStandardItem) which I think is quite 
powerful and we should encourage developers to make use of that.

Cheers,
Carl 
 
> 
> Thank you in advance.
> 
> Kind regards,
> László






More information about the Development mailing list