MVC架构之delegate
Qt的MVC架构可以实现很多数据显示的功能,本次主要对代理进行一个总结:
重实现QStyledItemDelegate类,实现自定义类。
(1)ComboxDelegate.h
#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H #include <QStyledItemDelegate> class ComboDelegate : public QStyledItemDelegate
{
public:
ComboDelegate(QObject *parent = NULL); protected:
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
}; #endif // COMBODELEGATE_H
(2)ComboDelegate.cpp
#include "ComboDelegate.h"
#include <QComboBox> ComboDelegate::ComboDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{} QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index); QStringList list;
list << "工人" << "农民" << "军人" << "律师"; QComboBox *pEditor = new QComboBox(parent);
pEditor->addItems(list);
pEditor->installEventFilter(const_cast<ComboDelegate*>(this));
return pEditor;
} void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString strText = index.model()->data(index).toString();
QComboBox *pCombox = NULL;
pCombox = static_cast<QComboBox*>(editor);
if (pCombox != NULL)
{
int nIndex = pCombox->findText(strText);
pCombox->setCurrentIndex(nIndex);
}
} void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const
{
QComboBox *pCombox = NULL;
pCombox = static_cast<QComboBox*>(editor);
if (pCombox != NULL)
{
QString strText = pCombox->currentText();
model->setData(index, strText);
}
} void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
(3)DateDelegate.h
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H #include <QStyledItemDelegate> class DateDelegate : public QStyledItemDelegate
{
public:
DateDelegate(QObject *parent = NULL); protected:
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
}; #endif // DATEDELEGATE_H
(4)DateDelegate.cpp
#include "DateDelegate.h"
#include <QDateTimeEdit> DateDelegate::DateDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{} // 首先创建要进行代理的窗体
QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index); QDateTimeEdit *pEditor = new QDateTimeEdit(parent); // 一个日历的控件
pEditor->setDisplayFormat("yyyy-MM-dd"); // 日期时间的显示格式
pEditor->setCalendarPopup(true); // 以下拉的方式显示
pEditor->installEventFilter(const_cast<DateDelegate*>(this)); // 调用这个函数安装事件过滤器,使这个对象可以捕获QDateTimeEdit对象的事件
return pEditor;
} // 这个是初始化作用,初始化代理控件的数据
void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
// 先用这个index返回这个model然后用这个model得到index对应的数据
QString strDate = index.model()->data(index).toString();
QDate date = QDate::fromString(strDate, Qt::ISODate); // 根据QString类型得到相应的时间类型
QDateTimeEdit *pEditor = NULL;
pEditor = static_cast<QDateTimeEdit*>(editor); // 强转为QDateTimeEdit*类型
if (pEditor != NULL)
{
pEditor->setDate(date); // 设置代理控件的显示数据
}
} // 将代理控件里面的数据更新到视图控件中
// void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QDateTimeEdit *pEditor = NULL;
pEditor = static_cast<QDateTimeEdit*>(editor); // 得到时间
if (pEditor != NULL)
{
QDate date = pEditor->date(); // 得到时间
model->setData(index, QVariant(date.toString(Qt::ISODate))); // 把值放到相应的index里面
}
} // 代理中数据的改变放到model中
// void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
(5)SpinDelegate.h
#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H #include <QStyledItemDelegate> class SpinDelegate : public QStyledItemDelegate
{
public:
SpinDelegate(QObject *parent = NULL); protected:
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
}; #endif // SPINDELEGATE_H
(6)SpinDelegate.cpp
#include "SpinDelegate.h" #include <QSpinBox> SpinDelegate::SpinDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
} QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index); QSpinBox *pEditor = new QSpinBox(parent);
pEditor->setRange(, );
pEditor->installEventFilter(const_cast<SpinDelegate*>(this));
return pEditor;
} void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int value = index.model()->data(index).toInt();
QSpinBox *pSpinbox = NULL;
pSpinbox = static_cast<QSpinBox*>(editor);
if (pSpinbox != NULL)
{
pSpinbox->setValue(value);
}
} void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QSpinBox *pSpinbox = NULL;
pSpinbox = static_cast<QSpinBox*>(editor);
if (pSpinbox != NULL)
{
int value = pSpinbox->value();
model->setData(index, value);
}
} void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index); editor->setGeometry(option.rect);
}
(7)main.cpp
#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QWidget>
#include <QTableView>
#include <QTextStream>
#include <QStandardItemModel>
#include "DateDelegate.h"
#include "ComboDelegate.h"
#include "SpinDelegate.h" int main(int argc, char *argv[])
{
QApplication a(argc, argv); QStandardItemModel model(, );
model.setHeaderData(, Qt::Horizontal, QLatin1String("Name"));
model.setHeaderData(, Qt::Horizontal, QLatin1String("Birthday"));
model.setHeaderData(, Qt::Horizontal, QLatin1String("Job"));
model.setHeaderData(, Qt::Horizontal, QLatin1String("Income")); QFile file(QLatin1String("/mnt/liuy/info"));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "open the file failed...";
return -;
} QTextStream out(&file);
QString line;
model.removeRows(, model.rowCount(QModelIndex()), QModelIndex());
int row = ;
do
{
line = out.readLine();
if (!line.isEmpty())
{
model.insertRows(row, , QModelIndex());
QStringList pieces = line.split(",", QString::SkipEmptyParts);
model.setData(model.index(row, , QModelIndex()), pieces.value());
model.setData(model.index(row, , QModelIndex()), pieces.value());
model.setData(model.index(row, , QModelIndex()), pieces.value());
model.setData(model.index(row, , QModelIndex()), pieces.value());
++row;
}
} while(!line.isEmpty());
file.close(); QTableView tableView;
tableView.setModel(&model); // 绑定Model
tableView.setWindowTitle(QLatin1String("Delegate")); DateDelegate dateDelegate;
tableView.setItemDelegateForColumn(, &dateDelegate); // 第一列代理
ComboDelegate comboDelegate;
tableView.setItemDelegateForColumn(, &comboDelegate);// 第二列代理
SpinDelegate spinDelegate;
tableView.setItemDelegateForColumn(, &spinDelegate); // 第三列代理 tableView.resize(, ); // 重置大小
tableView.show(); return a.exec();
}
信息文件
文件info 内容如下(注意:文件格式):
Liu,1977-01-05,工人,1500
Wang,1987-11-25,医生,2500
Sun,1967-10-05,军人,500
Zhang,1978-01-12,律师,4500
运行效果图如下:

第二列编辑图(时间日期控件):

第三列编辑图(下拉框控件):

第四列编辑图(微调框控件):

引用:http://www.cnblogs.com/Braveliu/p/7488250.html
未完待续...
MVC架构之delegate的更多相关文章
- MVC架构杂谈
来源:伯乐在线专栏作者 - 林欣达 链接:http://ios.jobbole.com/86895/ 点击 → 了解如何加入专栏作者 前言 MVC是软件工程中的一种软件架构模式,它把软件系统分为三个基 ...
- 【JAVA】基于MVC架构Java技术荟萃案例演练
基于JAVA-MVC技术的顾客管理项目案例总结 作者 白宁超 2016年6月9日22:47:08 阅读前瞻:本文源于对javaweb相关技术和资料汇总,涉及大量javaweb基础技术诸如:Servle ...
- Android 四大组件 与 MVC 架构模式
作为一个刚从JAVA转过来的Android程序员总会思考android MVC是什么样的? 首先,我们必须得说Android追寻着MVC架构,那就得先说一下MVC是个啥东西! 总体而来说MVC不能说是 ...
- MVC架构模式分析与设计(一)---简单的mvc架构
首先 我要感谢慕课网的老师提供视频资料 http://www.imooc.com/learn/69 下面我来进行笔记 我们制作一个简单的mvc架构 制作第一个控制器 testController.cl ...
- IntelliMVCCode智能MVC架构的代码助手使用方法
智能代码生成工具,快速帮助开发者提升开发速度,通过工具自动生成MVC架构的大量源代码,节省更多的开发时间. 工具使用的框架:.net4.0,通过工具连接到数据库自动提取数据表或视图中的结构,生成对应的 ...
- 从MVC框架看MVC架构的设计
尽管MVC早已不是什么新鲜话题了,但是从近些年一些优秀MVC框架的设计上,我们还是会发现MVC在架构设计上的一些新亮点.本文将对传统MVC架构中的一些弊病进行解读,了解一些优秀MVC框架是如何化解这些 ...
- 【PHP小项目使用MVC架构】
小项目名称是雇员管理系统. mvc是一种项目的开发模式,中文名称为模式视图控制器,是强制程序员将数据的输入.处理.输出分开的一种开发模式. 在这个小项目中,控制器使用service作为后缀名. 项目u ...
- 一个初学者对于MVC架构的理解
我很早之前就开始接触.NET开发,一直都在2.0的框架下,所以对于MVC这种架构,听说过,但没有具体使用过,近期和外部朋友接触时,有了解到他们公司在使用MVC这种架构,所以自己就找来相关资料了解一下M ...
- java MVC架构-spring mvc,struct2(理解)
MVC架构实现基础: 基于filter或者servlet实现请求地址分析,如果需要控制类处理请求,则调用相应的控制类.调用控制类时,根据配置文件初始化控制类相关的参数.数据库连接可持久化存在.控制类处 ...
随机推荐
- 用 Python 理解 Web 并发模型
用 Python 理解 Web 并发模型 http://www.jianshu.com/users/1b1fde012122/latest_articles 来源:MountainKing 链接: ...
- 【日常学习】【搜索/递归】codevs2802 二的幂次方题解
转载请注明出处 [ametake版权全部]http://blog.csdn.net/ametake欢迎来看 题目描写叙述 Description 不论什么一个正整数都能够用2的幂次方表示. 比如:13 ...
- 用Thinphp发送电子邮件的方法
好长时间没有动php了,突然想用thinkphp发送电子邮件,可是查阅了书籍都写的非常乱.没有继续看下去.这里找到了一个比較好的方法: 第一步:首先我们要引入一个外部类库:Mail.class.php ...
- 数据挖掘十大经典算法--CART: 分类与回归树
一.决策树的类型 在数据挖掘中,决策树主要有两种类型: 分类树 的输出是样本的类标. 回归树 的输出是一个实数 (比如房子的价格,病人呆在医院的时间等). 术语分类和回归树 (CART) 包括了上述 ...
- Zookeeper体系结构
上面我们已经讨论了zookeeper在应用程序中的一些操作,以下我们须要理解一下服务端的工作的原理.client是怎样通过一个client的类库与服务端进行通信的,然后服务端又是怎样回应client的 ...
- android app 架构设计01
1:本文有摘抄, 1 2 3 4 5 - 开发过程中.需求.设计.编码的一致性 - 整个程序具有统一的风格,比方对话框样式,button风格,色调等UI元素 - 整个程序详细统一的结构,比方不同模块訪 ...
- 软件-集成开发环境:IDE
ylbtech-软件-集成开发环境:IDE 集成开发环境(IDE,Integrated Development Environment )是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器. ...
- 利用道格拉斯·普客法(DP法)压缩矢量多边形(C++)
1.算法描述 经典的Douglas-Peucker算法(简称DP法)描述如下: (1)在曲线首尾两点A,B之间连接一条直线AB,该直线为曲线的弦: (2)得到曲线上离该直线段距离最大的点C,计算其与A ...
- Centos7 docker nginx容器搭建
一.安装docker http://www.cnblogs.com/WJ--NET/p/8553807.html 二.创建Dockerfile #创建文件夹 mkdir centos_nginx cd ...
- 4.Projects and Scenes介绍
1.Project 一个项目是由一系列的文件(如图片.音频.几何).场景以及vzp文件组成.这些文件被导入到项目对应的文件夹中.项目外部资源在场景中被使用后,会导入项目中,除非该资源被标记为外部引用. ...