In my raster drawing program I need to create a layers interface like in Photoshop or Sketchbook Pro. I read the documentation and figured out that I have to use QTreeView. But I didn’t find a lot of information in the documentation about creating QTreeView with custom widgets. So:
1) How to insert custom widgets into tree view?
2) What is the difference between QTreeView and QTreeWidget?
3) What is the difference between QAbstractItemModel and qitemdelegate?
4) Any examples/articles/guides?
5) Maybe I should use something else?
2
Answers
QTreeWidget
is a model and a view in one class, it’s called a convenience view. It works against the good practice of separatng the views and the models, and probably shouldn’t be used in a system where the notion of document layers belongs in the document handling code.QTreeView
is just a view, without any bundled models. When you have a model, you can set it on a view, making the view display the model.A
QAbstractItemModel
is the data model. It has nothing to do with views or delegates at all – the model can exist and be useful without a view at all.A delegate provides display and editing widgets for items of data in a view. It is a property of the view, not of the model. Different views can show the same model using different delegates, all at the same time.
Although the delegate lets you provide the custom widgets you’re after, its use may be unnecessary. If the item you display has static contents, you can simply provide a
QImage
or aQPixmap
as the data.Special for your case (5): DON’T use any of
QTreeView
,QStandardItemModel
and other such classes. If you need interaction with widgets + if you need widgets to be animated then you should use simpleQScrollArea
withQVBoxLayout
inside of it.Qt MVC is designed to process big amount of cognate data. It is not designed to provide widget-based interaction. So, if you want to “assign” one widget to each item and to interact with them – you will have a lot of problems with implementing delegates (tracking mouse events, providing editor’s factory). Ofc, you may create your own delegates with custom drawing and custom processing of mouse events, but it’s much easy to use simple widgets.