【Qt5开发及实例】25、实现代理的功能

来源:互联网 发布:特战先驱 知乎 编辑:程序博客网 时间:2024/06/05 09:40

实现代理的功能

在Qt里面也有MVC,那就是视图,模型,代理,后面我们再开一章,好好来学习一下Qt的MVC吧!


main.cpp

/*** 书本:【Qt5开发及实例】* 功能:实现代理的功能* 文件:main.cpp* 时间:2015年1月29日20:53:04* 作者:cutter_point*/#include <QApplication>#include <QStandardItemModel>#include <QTableView>//#include <QFileDialog>#include <QFile>#include <QTextStream>#include "datedelegate.h"#include "combodelegate.h"#include "spindelegate.h"int main(int argc, char *argv[]){  QApplication a(argc, argv);  QStandardItemModel model(4, 4);     //一个表格模型  QTableView tableView;     //一个表的视图  tableView.setModel(&model);    //把这个模型设置为这个视图的模型  //---------------------------------------------------------------------------------------------------------------  //设定代理  DateDelegate dateDelegate;  ComboDelegate comboDelegate;  spindelegate spinDelegate;  tableView.setItemDelegateForColumn(1, &dateDelegate);   //第二列设定代理  tableView.setItemDelegateForColumn(2, &comboDelegate);  tableView.setItemDelegateForColumn(3, &spinDelegate);  model.setHeaderData(0, Qt::Horizontal, QObject::tr("名字"));      //名字  model.setHeaderData(1,Qt::Horizontal,QObject::tr("生日"));  model.setHeaderData(2,Qt::Horizontal,QObject::tr("职业"));  model.setHeaderData(3,Qt::Horizontal,QObject::tr("收入"));        //第一列//  QString path = QFileDialog::getOpenFileName(tableView, "Open", ".", "file(*.txt)");  QFile file("F:/QTcode/zhangjie8/CH803/test.txt");//  QFile file("../test.txt");  if(file.open(QFile::ReadOnly | QFile::Text))      //打开文件为只读和文本形式    {      QTextStream stream(&file);      QString line;   //读取一行      model.removeRows(0, model.rowCount(QModelIndex()), QModelIndex());    //去除所有的数据清空      int row = 0;      do        {          line = stream.readLine();   //读取一行数据          if(!line.isEmpty())            {              model.insertRows(row, 1, QModelIndex());      //为当前这个索引插入一行,1个数据              QStringList pieces = line.split(",", QString::SkipEmptyParts);              //接下来依次插入数据              model.setData(model.index(row,0,QModelIndex()),pieces.value(0));              model.setData(model.index(row,1,QModelIndex()),pieces.value(1));              model.setData(model.index(row,2,QModelIndex()),pieces.value(2));              model.setData(model.index(row,3,QModelIndex()),pieces.value(3));              row++;            }        }while(!line.isEmpty());      file.close();    }  tableView.setWindowTitle(QObject::tr("Delegate"));  tableView.show();  return a.exec();}


datedelegate.h

/*** 书本:【Qt5开发及实例】* 功能:实现代理的功能* 文件:spindelegate.h* 时间:2015年1月30日17:48:38* 作者:cutter_point*/#ifndef SPINDELEGATE_H#define SPINDELEGATE_H#include <QItemDelegate>class spindelegate : public QItemDelegate{  Q_OBJECTpublic:  explicit spindelegate(QObject *parent = 0);signals:public slots:  // QAbstractItemDelegate interfacepublic:  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

datedelegate.cpp

/*** 书本:【Qt5开发及实例】* 功能:实现代理的功能* 文件:main.cpp* 时间:2015年1月30日16:49:58* 作者:cutter_point*/#include "datedelegate.h"#include <QDateTimeEdit>DateDelegate::DateDelegate(QObject *parent) : QItemDelegate(parent){//代理类}//首先创建要进行代理的窗体QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{  QDateTimeEdit *editor = new QDateTimeEdit(parent);      //一个日历的控件  editor->setDisplayFormat("yyyy-MM-dd");   //日期时间的显示格式  editor->setCalendarPopup(true);   //以下拉的方式显示  editor->installEventFilter(const_cast<DateDelegate*>(this));    //调用这个函数安装事件过滤器,使这个对象可以捕获QDateTimeEdit对象的事件  return editor;}//这个是初始化作用,初始化代理控件的数据void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{  //先用这个index返回这个model然后用这个model得到index对应的数据  QString dateStr = index.model()->data(index).toString();  QDate date = QDate::fromString(dateStr, Qt::ISODate);     //根据QString类型得到相应的时间类型  QDateTimeEdit *edit = static_cast<QDateTimeEdit*>(editor);    //强转为QDateTimeEdit*类型  edit->setDate(date);      //设置代理控件的显示数据}//将代理控件里面的数据更新到视图控件中//void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;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)));    //把值放到相应的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{  editor->setGeometry(option.rect);}

combodelegate.h

/*** 书本:【Qt5开发及实例】* 功能:实现代理的功能* 文件:combodelegate.h* 时间:2015年1月30日17:48:38* 作者:cutter_point*/#ifndef COMBODELEGATE_H#define COMBODELEGATE_H#include <QItemDelegate>class ComboDelegate : public QItemDelegate{  Q_OBJECTpublic:  explicit ComboDelegate(QObject *parent = 0);signals:public slots:  // QAbstractItemDelegate interfacepublic:   //这里同datedelegate.h里面的意思  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

combodelegate.cpp

/*** 书本:【Qt5开发及实例】* 功能:实现代理的功能* 文件:main.cpp* 时间:2015年1月30日16:49:58* 作者:cutter_point*/#include "datedelegate.h"#include <QDateTimeEdit>DateDelegate::DateDelegate(QObject *parent) : QItemDelegate(parent){//代理类}//首先创建要进行代理的窗体QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{  QDateTimeEdit *editor = new QDateTimeEdit(parent);      //一个日历的控件  editor->setDisplayFormat("yyyy-MM-dd");   //日期时间的显示格式  editor->setCalendarPopup(true);   //以下拉的方式显示  editor->installEventFilter(const_cast<DateDelegate*>(this));    //调用这个函数安装事件过滤器,使这个对象可以捕获QDateTimeEdit对象的事件  return editor;}//这个是初始化作用,初始化代理控件的数据void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{  //先用这个index返回这个model然后用这个model得到index对应的数据  QString dateStr = index.model()->data(index).toString();  QDate date = QDate::fromString(dateStr, Qt::ISODate);     //根据QString类型得到相应的时间类型  QDateTimeEdit *edit = static_cast<QDateTimeEdit*>(editor);    //强转为QDateTimeEdit*类型  edit->setDate(date);      //设置代理控件的显示数据}//将代理控件里面的数据更新到视图控件中//void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;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)));    //把值放到相应的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{  editor->setGeometry(option.rect);}

spindelegate.h

/*** 书本:【Qt5开发及实例】* 功能:实现代理的功能* 文件:spindelegate.h* 时间:2015年1月30日17:48:38* 作者:cutter_point*/#ifndef SPINDELEGATE_H#define SPINDELEGATE_H#include <QItemDelegate>class spindelegate : public QItemDelegate{  Q_OBJECTpublic:  explicit spindelegate(QObject *parent = 0);signals:public slots:  // QAbstractItemDelegate interfacepublic:  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

spindelegate.cpp

/*** 书本:【Qt5开发及实例】* 功能:实现代理的功能* 文件:spindelegate.cpp* 时间:2015年1月30日17:48:38* 作者:cutter_point*/#include "spindelegate.h"#include <QSpinBox>spindelegate::spindelegate(QObject *parent) :  QItemDelegate(parent){}QWidget *spindelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{  QSpinBox *editor = new QSpinBox(parent);  editor->setRange(0, 30000);  editor->installEventFilter(const_cast<QSpinBox*>(editor));  return editor;}void spindelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{  int value = index.model()->data(index).toInt();  QSpinBox *box = static_cast<QSpinBox*>(editor);  box->setValue(value);}void spindelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{  QSpinBox *box = static_cast<QSpinBox*>(editor);  int value = box->value();  model->setData(index, value);}void spindelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{  editor->setGeometry(option.rect);}

图形显示













0 0