代理Delegate的小应用(使用setModelData设置下拉日期对话框)
2016-11-07 08:02
435 查看
前言
在平时关于表格一类的的控件使用中,不可避免需要修改每个Item的值,通过在Item中嵌入不同的控件对编辑的内容进行限定,然而在表格的Item中插入的控件始终显示,当表格中item项很多的时候,会影响表格的美观和用户的体验。此时Delegate代理就派上了用场,通过Delegate可以使得Item的控件在编辑状态才显示,提高了用户的体验满意度。效果展示
1、展示状态:2、编辑状态
设计思路
这类效果的实现主要使用了QItemDelegate类,QItemDelegate类为数据项Item的显示和编辑提供了一套工具。QItemDelegate类可以用来Item项的常规性展示,也可以为基于QAbstractItemView 的控件的Item提供编辑的功能。QItemDelegate是Model/View的框架的一部分。
可以通过实现以下四个函数来为Item实现基本的编辑功能。
QWidget *createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;//返回改变Model数据的widget,该widget是经过定制行为的Widget void setEditorData(QWidget * editor, const QModelIndex & index) const;//将可操作的数据提供给widget void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const; //将widget的数据展示到Item中 void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;//确保widget能够正确显示到view中
从介绍的四个虚函数可以看出,通过实现这四个函数,可以基本实现表格类控件的ITem的编辑功能,以下将以日历控件来对这些函数的实现做一个介绍,其他的控件甚至是自定义控件,通过照猫画虎,相信可以达到自己的目的。
QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { QDateTimeEdit *editer = new QDateTimeEdit(parent); editer->setDisplayFormat("yyyy-MM-dd");//设置显示格式 editer->setCalendarPopup(true);//设置日历控件为悬空显示 editer->installEventFilter(const_cast<DateDelegate*>(this));//安装时间过滤器,使得代理能够获取定制控件的值 return editer; }
void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString dateStr = index.model()->data(index).toString();//通过index获取model中相应项ITem的值 QDate date = QDate::fromString(dateStr,Qt::ISODate); QDateTimeEdit *edit = static_cast<QDateTimeEdit *>(editor);//转换类型获取定制的控件 edit->setDate(date);//将Item项中的值展示到定制控件中 }
void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QDateTimeEdit *edit = static_cast<QDateTimeEdit *>(editor); QDate date = edit->date(); model->setData(index,QVariant(date.toString(Qt::ISODate)));//将定制控件中的值展示到表格中 }
//调整定制控件的展示,确保可以展示到窗体View中 void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); }
总结
好了,通过以上的介绍,通过自定义类实现QItemDelegate类,可以使用定制的控件实现表格类控件的展示和编辑功能无缝切换,提高用户体验。该链接是本人试验的简单的代码,欢迎各位交流技术,互相提高!
http://blog.csdn.net/sxpsxp12/article/details/52402496
相关文章推荐
- 代理Delegate的小应用(使用setModelData设置下拉日期对话框)
- c#格式化数据 日期格式设置示例 format使用 DataFormatString使用
- jqGrid与Struts2的结合应用(六) —— 使用colModel设置查询功能
- jqGrid与Struts2的结合应用(六) ------ 使用colModel设置查询功能 .
- confirm()对话框在.net的webimagebutton的使用,下拉日期WebDataInput的使用
- jqGrid与Struts2的结合应用(六) —— 使用colModel设置查询功能
- jqGrid与Struts2的结合应用(六) —— 使用colModel设置查询功能
- asp.net2.0 c#格式化数据 日期格式设置示例 format使用 DataFormatString使用
- confirm()对话框在.net的button中的使用,下拉日期Calendar的使用
- jqGrid与Struts2的结合应用(六) —— 使用colModel设置查询功能
- jqGrid与Struts2的结合应用(六) —— 使用colModel设置查询功能
- jqGrid与Struts2的结合应用(六) —— 使用colModel设置查询功能
- jqGrid与Struts2的结合应用(六) —— 使用colModel设置查询功能
- javascript的高级使用,设置页面元素是否相应事件与显示“正在处理”对话框
- ASP.NET的gridview设置数据格式(DataFormatString="{}")与 String.Format 使用总结
- 在DataGrid中使用下拉列表框和设置焦点
- 使用SetLayeredWindowAttributes函数设置窗口透明色与透明度
- 使用set命令设置环境变量
- 开源:.NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现与应用(2)
- C#2.0中 GridView日期列使用DataFormatString="{0:d}"