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. 【ACM】nyoj_305_表达式求值_201308081018

    表达式求值时间限制:3000 ms  |  内存限制:65535 KB 难度:3描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min(20, ...

  2. 【ACM】hdu_zs2_1005_Problem E _201308030747

    Problem E Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Total Subm ...

  3. [bzoj2124]等差子序列_线段树_hash

    等差子序列 bzoj-2124 题目大意:给定一个1~n的排列,问是否存在3个及以上的位置上的数构成连续的等差子序列. 注释:$1\le n\le 10^4$. 想法:这题就相当于是否存在3个数i,j ...

  4. Git的基本设置

    进入虚拟机环境中:首先我们对 Git 进行用户名和邮箱进行设置,请参照下面格式,替换为你自己常用的用户名和邮箱来完成设置: $ git config --global user.name " ...

  5. chcp - 设置或者显示活动代码页编号

    chcp - 设置或者显示活动代码页编号 学习了:https://baike.baidu.com/item/CHCP/9061635?fr=aladdin

  6. Keil5.15使用GCC编译器链接.a库文件

    我们知道,当使用第三方的代码时,人家有可能会扔个Lib文件给你.这时候,别人仅仅要提供header文件给你,则你就能够通过Lib文件及header的函数声明,对Lib中的函数进行调用.在Keil中假设 ...

  7. 设计一部iphone手机用面向对象的方法

    main.m //编辑字体大小command + < //编译执行快捷键 com + R #import <Foundation/Foundation.h> #import &quo ...

  8. SQLServer2012 表IAM存储结构探究

    SQLServer2012 表IAM存储结构探究 Author:zfive5(zidong) Email: zfive5@163.com 引子 国庆节期间,一直在翻阅<程序猿的自我修养-链接.装 ...

  9. Java中利用随机数的猜拳游戏

    Java中利用随机数的猜拳游戏,实现非常简单,重难点在于随机数的产生. 首先GameJude类是用于判断输赢的一个类: package testGame; public class GameJudge ...

  10. BZOJ 1061费用流

    思路: 我们可以列出几个不等式 用y0带进去变成等式 下-上 可以消好多东西 我们发现 等式左边的加起来=0 可以把每个方程看成一个点 正->负 连边 跑费用流即可 //By SiriusRen ...