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的更多相关文章

  1. MVC架构杂谈

    来源:伯乐在线专栏作者 - 林欣达 链接:http://ios.jobbole.com/86895/ 点击 → 了解如何加入专栏作者 前言 MVC是软件工程中的一种软件架构模式,它把软件系统分为三个基 ...

  2. 【JAVA】基于MVC架构Java技术荟萃案例演练

    基于JAVA-MVC技术的顾客管理项目案例总结 作者 白宁超 2016年6月9日22:47:08 阅读前瞻:本文源于对javaweb相关技术和资料汇总,涉及大量javaweb基础技术诸如:Servle ...

  3. Android 四大组件 与 MVC 架构模式

    作为一个刚从JAVA转过来的Android程序员总会思考android MVC是什么样的? 首先,我们必须得说Android追寻着MVC架构,那就得先说一下MVC是个啥东西! 总体而来说MVC不能说是 ...

  4. MVC架构模式分析与设计(一)---简单的mvc架构

    首先 我要感谢慕课网的老师提供视频资料 http://www.imooc.com/learn/69 下面我来进行笔记 我们制作一个简单的mvc架构 制作第一个控制器 testController.cl ...

  5. IntelliMVCCode智能MVC架构的代码助手使用方法

    智能代码生成工具,快速帮助开发者提升开发速度,通过工具自动生成MVC架构的大量源代码,节省更多的开发时间. 工具使用的框架:.net4.0,通过工具连接到数据库自动提取数据表或视图中的结构,生成对应的 ...

  6. 从MVC框架看MVC架构的设计

    尽管MVC早已不是什么新鲜话题了,但是从近些年一些优秀MVC框架的设计上,我们还是会发现MVC在架构设计上的一些新亮点.本文将对传统MVC架构中的一些弊病进行解读,了解一些优秀MVC框架是如何化解这些 ...

  7. 【PHP小项目使用MVC架构】

    小项目名称是雇员管理系统. mvc是一种项目的开发模式,中文名称为模式视图控制器,是强制程序员将数据的输入.处理.输出分开的一种开发模式. 在这个小项目中,控制器使用service作为后缀名. 项目u ...

  8. 一个初学者对于MVC架构的理解

    我很早之前就开始接触.NET开发,一直都在2.0的框架下,所以对于MVC这种架构,听说过,但没有具体使用过,近期和外部朋友接触时,有了解到他们公司在使用MVC这种架构,所以自己就找来相关资料了解一下M ...

  9. java MVC架构-spring mvc,struct2(理解)

    MVC架构实现基础: 基于filter或者servlet实现请求地址分析,如果需要控制类处理请求,则调用相应的控制类.调用控制类时,根据配置文件初始化控制类相关的参数.数据库连接可持久化存在.控制类处 ...

随机推荐

  1. ExtJs之Ext.Model的MemoryProxy

    书上的代码已完全不可参考,只好按知识点从网上查资料一个一个实例 了. <!DOCTYPE html> <html> <head> <title>ExtJ ...

  2. C#中数组的使用

    1.简单数组: 声明: int[] myArray 初始化: 声明了数组后,就必须为数组分配内存来保存数组的全部元素.特别注意的是数组是引用类型.故须要使用new运算符来指定数组中元素的类型和数量来初 ...

  3. jplogic v1.0案例开发之知识库管理(文档管理等)(二)

    jplogic v1.0开发陆续更新,意在和广大网友分享交流.通过寻求合作伙伴,交流群.以下是jplogic的关于知识库模块的部分功能,例如以下进行功能展示: 知识库主界面: 新增知识类别: wate ...

  4. 【Java】基本类型和引用类型(值传递)

    [关键词] [问题] · 加深对基本类型和引用类型的理解: [效果图] [分析] 參见最后的[參考资料] [解决方式] [代码] public void test() throws Exception ...

  5. 《游戏脚本的设计与开发》-(RPG部分)3.8 通过脚本来自由控制游戏(一)

    注意:本系列教程为长篇连载无底洞.半路杀进来的朋友,假设看不懂的话.请从第一章開始看起.文章文件夹请点击以下链接. http://blog.csdn.net/lufy_legend/article/d ...

  6. nginx源代码分析--nginx进程间通信

    Linux下的IPC非常多,nginx的进程都是有亲缘关系的进程,对于他们的通信我们选择TCP socket进行通信.   TCP socket 用来做进程通信的优点有,   1.socket是文件描 ...

  7. js面试题--js的继承

    js是门灵活的语言,实现一种功能往往有多种做法,ECMAScript没有明白的继承机制.而是通过模仿实现的.依据js语言的本身的特性,js实现继承有下面通用的几种方式 1.使用对象冒充实现继承(该种实 ...

  8. JavaScript和Java的区别

    一个是编译型语言(客户端平台必须有仿真器或解释器),一个是解析型语言(不经过编译,直接将文本格式的字符代码发送给浏览器解释执行). 虽然JavaScript与Java有紧密的联系,但却是两个公司开发的 ...

  9. Linux - 控制台界面,虚拟界面,字符界面

    tty控制台终端. pts虚拟终端. tty1 图形界面. tty2 字符界面. Ctrl+Alt+F2-6 在字符界面下,通过Alt+F2 切换回来.或者切换到其他的字符界面. Alt+F2 pts ...

  10. yii1 session

    在 Yii框架中使用session 的笔记: 首先,在Yii框架中,你不需要像标准PHP代码那样使用session_start(),在Yii框架中,autoStart 属性缺省被设置为true,所以, ...