您的位置:首页 > 编程语言 > Qt开发

qt 委托 delegate

2015-10-01 08:38 387 查看
委托的作用是,显示和编辑视图view中的item。

大多数情况下,view视图中的默认delegate委托已经足够了。一般不需要自定义委托。

委托的显示,是显示在view上的。

编辑,是针对model的,需要修改model中的数据。

如果项更好的控制有关项item的显示,可用在自定义model中实现,data()函数中,根据不同的role,返回不同的值。

比如:Qt::FontRole,Qt::DisplayRole等角色。

data()函数会自动的被委托调用。

这样,也能够更好的控制item的显示。

而且,也不需要实现一个自定义的委托。

如果想要更多的控制,比如在一个view中的item中,显示一个按钮,或者一个spinBox,或者其他的控件。

那么这个时候,可用使用delegate自定义委托。

QItemDelegate can be used to provide custom display features and editor widgets for item

We recommend the use of QStyledItemDelegate when creating new delegates.

更建议使用QStyleItemDelegate类,当自定义委托的时候。

When editing data in an item view, QItemDelegate provides an editor widget, which is a widget that is placed on top of the view while editing takes place

Editors are created with a QItemEditorFactory; a default static instance provided by QItemEditorFactory is installed on all item delegates.

使用委托的视图。当只是显示数据的时候,调用的委托的paint()方法,在paint()中,调用的是drawDisplay()方法,该方法只是显示text的。

只有在用户编辑item的时候,才会调用委托中的editor widget。即用户编辑item的时候,使用的是委托中的editor widget控件。这时候,view中显示的

才是editor widget控件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: