简体   繁体   English

使用自定义小部件自定义QT QTreeView

[英]Customizing QT QTreeView with custom widgets

In my raster drawing program I need to create a layers interface like in Photoshop or Sketchbook Pro. 在我的光栅绘图程序中,我需要创建一个像Photoshop或Sketchbook Pro中的图层界面。 I read the documentation and figured out that I have to use QTreeView. 我阅读了文档并发现我必须使用QTreeView。 But I didn't find a lot of information in the documentation about creating QTreeView with custom widgets. 但是我没有在文档中找到有关使用自定义小部件创建QTreeView的大量信息。 So: 所以:

1) How to insert custom widgets into tree view? 1)如何将自定义小部件插入树视图?
2) What is the difference between QTreeView and QTreeWidget? 2)QTreeView和QTreeWidget有什么区别?
3) What is the difference between QAbstractItemModel and qitemdelegate? 3)QAbstractItemModel和qitemdelegate有什么区别?
4) Any examples/articles/guides? 4)任何示例/文章/指南?
5) Maybe I should use something else? 5)也许我应该用别的东西?

QTreeWidget is a model and a view in one class, it's called a convenience view. QTreeWidget是一个类中的模型和视图,它被称为便捷视图。 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. QTreeView只是一个视图,没有任何捆绑的模型。 When you have a model, you can set it on a view, making the view display the model. 拥有模型时,可以在视图上进行设置,使视图显示模型。

A QAbstractItemModel is the data model. QAbstractItemModel是数据模型。 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. 如果您显示的项目具有静态内容,则只需提供QImageQPixmap作为数据。

Special for your case (5): DON'T use any of QTreeView , QStandardItemModel and other such classes. 特殊情况(5):不要使用任何QTreeViewQStandardItemModel和其他类似的类。 If you need interaction with widgets + if you need widgets to be animated then you should use simple QScrollArea with QVBoxLayout inside of it. 如果你需要与小部件交互+如果你需要动画小部件,那么你应该使用简单的QScrollAreaQVBoxLayout

Qt MVC is designed to process big amount of cognate data. Qt MVC旨在处理大量的同源数据。 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. Ofc,您可以使用自定义绘图和鼠标事件的自定义处理创建自己的代理,但使用简单的小部件非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM