自定义委托(代理)SearchDelegate ------ Model-View-Control(MVC)架构(6)
2014-07-21 08:41
483 查看
现在,模型、视图、数据条目都有了,最后再添加委托(Delegate,也叫代理)。
代理主要完成数据条目的呈现、渲染。所以其中两个最重要的函数就是paint和sizeHint,paint函数负责数据条目的具体呈现,在哪里画个图标,或者在哪里写几个字,都在这里完成。sizeHint用以通知Qt系统当前的数据条目的尺寸。
这2个函数里用到的数据,都来自于自定义的数据条目SearchItem,这个可以参见上一篇博客。
具体代码如下:
search_delegate.h
search_delegate.cpp
至此,模型、视图(数据条目)、委托全部完成。编译、运行测试,虽然没报错,但是发现,视图里空空如也,啥也没用:
为什么会是这样?问题留待下一篇博客解决。
代理主要完成数据条目的呈现、渲染。所以其中两个最重要的函数就是paint和sizeHint,paint函数负责数据条目的具体呈现,在哪里画个图标,或者在哪里写几个字,都在这里完成。sizeHint用以通知Qt系统当前的数据条目的尺寸。
这2个函数里用到的数据,都来自于自定义的数据条目SearchItem,这个可以参见上一篇博客。
具体代码如下:
search_delegate.h
#ifndef SEARCH_DELEGATE_H #define SEARCH_DELEGATE_H #include <QStyledItemDelegate> class SearchDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit SearchDelegate(QObject *parent = 0); void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const; }; #endif // SEARCH_DELEGATE_H
search_delegate.cpp
#include "search_delegate.h" #include "search_item.h" #include <QPainter> #include <QBrush> #include <QDebug> SearchDelegate::SearchDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void SearchDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { qDebug()<<"paint"; //如果是自定义的item,则用以下方法绘制 if(index.data().canConvert<SearchItem*>()) { QStyledItemDelegate::paint(painter, option, index); painter->save(); SearchItem *item=qvariant_cast<SearchItem*>(index.data()); qDebug()<<item->nickname; item->paintSelf(painter, option.rect); painter->restore(); } //否则,用默认方法绘制(实际上,我们全部是自定义的item,但这个也必须要写上) else { QStyledItemDelegate::paint(painter, option, index); } } QSize SearchDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { if(index.data().canConvert<SearchItem*>()) { SearchItem *item=qvariant_cast<SearchItem*>(index.data()); return item->sizeHint(); } else { return QStyledItemDelegate::sizeHint(option, index); } }
至此,模型、视图(数据条目)、委托全部完成。编译、运行测试,虽然没报错,但是发现,视图里空空如也,啥也没用:
为什么会是这样?问题留待下一篇博客解决。
相关文章推荐
- 自定义SearchItem类 ------ Model-View-Control(MVC)架构(5)
- 自定义模型SearchModel ------ Model-View-Control(MVC)架构(8)
- 自定义模型SearchModel ------ Model-View-Control(MVC)架构(9)
- 各种模型的比较 ------ Model-View-Control(MVC)架构(2)
- QAbstractTableModel ------ Model-View-Control(MVC)架构(3)
- QListView ------ Model-View-Control(MVC)架构(1)
- Qt MVC View Model Control /Delegate
- QTableView ------ Model-View-Control(MVC)架构(4)
- 自定义模型SearchModel ------ Model-View-Control(MVC)架构(7)
- 自定义MVC视图引擎ViewEngine 创建Model的专属视图
- 用C#实现MVC(Model View Control)模式介绍
- Pyqt Model/view框架 2.自定义委托(Delegate)
- ASP.NET MVC自定义视图引擎ViewEngine 创建Model的专属视图
- 设置UIWebView为自定义UIScrollViewDelegate代理冲突问题
- 用C#实现MVC(Model View Control)模式介绍
- 0819-TableView(tableVeiw控件的代理)(tableView单组 lol数据展示)(tableView-汽车品牌logo 右侧a-z)(KVC)(tableView - 添加删除按钮出现)(自定义代理delegate)(内存)
- MVC(Model-View-Control)
- C#实现MVC(Model View Control)模式介绍
- qt利用委托:QStyledItemDelegate,实现tableview 的表格项的自定义显示,如插入图片,绘制控件
- 用C#实现MVC(Model View Control)模式介绍