【1】代理应用示例源码

用代码说事,比较靠谱。

代码目录:三个自定义类,重实现QStyledItemDelegate类。main函数应用示例。

(1)ComboDelegate.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();
}

备注:此示例运行环境为UBuntu + Qt5.3.2

【2】读取的信息文件

文件info 内容如下(注意:文件格式):

Liu,--,工人,
Wang,--,医生,
Sun,--,军人,
Zhang,--,律师,

【3】运行效果图

运行效果图:

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

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

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

Good Good Study, Day Day Up.

顺序 选择 循环 总结

Qt—MVC架构的更多相关文章

  1. MVC架构之delegate

    Qt的MVC架构可以实现很多数据显示的功能,本次主要对代理进行一个总结: 重实现QStyledItemDelegate类,实现自定义类. (1)ComboxDelegate.h #ifndef COM ...

  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. Nand Flash 驱动框架

    框架入口源文件:s3c_nand.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6   硬件平台:JZ2440 以下是驱动框架: 以下是驱动代码 s3c_nan ...

  2. SpringBoot-热部署Devtools

    热部署 什么是热部署 所谓的热部署:比如项目的热部署,就是在应用程序在不停止的情况下,实现新的部署 项目演示案例 @RestController @Slf4j public class IndexCo ...

  3. Centos6.5+Redmine

    花了两天时间,基于centos6.5操作系统,搭建了redmine环境,在这里记录下过程中遇到的问题以及搭建流程. centos6.5; redmine2.5.0; Ruby1.9.3; step 1 ...

  4. dba工作内容

    一.数据库管理员的工作内容 关键词:dba工作内容 转自:http://blog.sina.com.cn/s/blog_44e0d0490102won1.html 1.规划与建设: 1.数据库服务器环 ...

  5. The iOS Simulator deployment target is set to 6.0

    XCODE警告 Showing All Messages :-1: The iOS Simulator deployment target is set to 6.0, but the range o ...

  6. what's the 单例模式

    what's the 单例模式 单例模式,是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例.即一个类只有一个对象实例 ...

  7. 008-docker-安装-tomcat:8.5.38-jre8

    1.搜索镜像 docker search tomcat 2.拉取合适镜像 查询tags:https://hub.docker.com/ docker pull tomcat:8.5.38-jre8 d ...

  8. error: Microsoft Visual C++ 14.0 is required(line_profiler模块安装失败的解决办法)

    一.我的安装环境: 1.系统:win10,64位 2.python版本:python3.6.4 二.遇到的问题: 1.cmd黑屏终端下输入命令:pip install line_profiler(安装 ...

  9. Spark学习:ShutdownHookManager虚拟机关闭钩子管理器

    Java程序经常也会遇到进程挂掉的情况,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码. JAVA中的ShutdownHook提供了比较好的方案. JDK提供了Jav ...

  10. Oracle查看用户密码过期,修改永不过期

    01.查看当前open用户 select username,account_status,expiry_date,profile from dba_users; 02.查看目前的密码过期策略 sele ...