4

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?

  • This question is a little too broad, it looks like you need to spend some more time reading through the Qt documentation. – Nathan Monteleone Mar 27 '14 at 18:02
  • I read the the Qt documentation. I wouldn't ask this if I'd found the answer in there. –  Mar 27 '14 at 18:49
  • If you use Qt Creator, go to the Welcome page, select the [Examples] section, and type `model` or `view` in the search bar. Select an example, and read the code. It's all there :) – Kuba hasn't forgotten Monica Mar 28 '14 at 06:07

2 Answers2

3

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 a QPixmap as the data.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
2

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 simple QScrollArea with QVBoxLayout 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.

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61