博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第五十六课、模型视图设计模式(中)------------------狄泰软件学院
阅读量:7036 次
发布时间:2019-06-28

本文共 2567 字,大约阅读时间需要 8 分钟。

一、模型视图设计模式

1、模型视图设计模式

(1)、模型定义标准接口(成员函数)对数据进行访问(例子中m_fileMode.data(root)等

(2)、视图通过标准接口获取数据并定义显示方式

(3)、模型使用信号与槽的机制通知视图数据变化(如上节课的动态显示

(4)、模型中的数据都是以层次结构表示的

2、模型中的索引

(1)、模型索引是将数据与视图分离的重要机制

(2)、模型中的数据使用唯一的索引来访问

(3)、QModelIndex是Qt中的模型索引类

A、包含具体数据的访问模式

B、包含一个指向模型的指针

3、索引的意义

4、索引中的行和列

(1)、线性模型可以使用(row、column)作为数据索引

5、模型中的通用树形结构

(1)、Root为虚拟节点,用于统一所有数据到同一棵树中

(2)、同一节点的子节点以递增的方式进行编号

(3)、通过(Index,parent)的方式确定节点

  

6、模型中数据索引的通用方式

(1)、三元组:(row,column,parent)

#ifndef MAINWINDOW_H#define MAINWINDOW_H#include 
#include
#include
class MainWindow : public QMainWindow{ Q_OBJECT QFileSystemModel m_fileModel; QPlainTextEdit m_edit;protected slots: void onDirectoryLoaded(const QString & path);public: MainWindow(QWidget *parent = 0); ~MainWindow();};#endif // MAINWINDOW_H
MainWindow.h
#include "MainWindow.h"#include 
#include
#include
#include
#include
#include
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ m_edit.setParent(this); m_edit.resize(600,300); m_edit.move(10,10); m_fileModel.setRootPath(QDir::currentPath()); connect(&m_fileModel, SIGNAL(directoryLoaded(QString)), this, SLOT(onDirectoryLoaded(QString)));}void MainWindow::onDirectoryLoaded(const QString & path){ QModelIndex root = m_fileModel.index(path);//用模型来提供索引,可以用通用的三元组的形式,但是也不是非得一定要 QByteArray array; QBuffer buffer(&array);//定义缓冲区 if(buffer.open(QIODevice::WriteOnly)) { QTextStream out(&buffer); out << m_fileModel.isDir(root) << endl; out << m_fileModel.data(root).toString() << endl; out << root.data().toString() << endl;//索引可以直达数据 out << &m_fileModel << endl; out << root.model() << endl;//索引有一个指向模型的指针 out << m_fileModel.filePath(root) << endl; out << m_fileModel.fileName(root) << endl; out << endl; for(int i=0; i
MainWindow.cpp
#include 
#include "MainWindow.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}
main.cpp
//输出结果1lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____0x28fe4c0x28fe4cF:/Qt/lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____debugMakefileMakefile.DebugMakefile.Releaserelease

二、小结

(1)、索引是访问模型中具体数据约定方式

(2)、获取索引中的通用方式为三元组(row,column,parent)

(3)、索引在需要时由模型实时创建

(4)、使用空索引作为父节点表示顶层数据元素

(5)、特殊的模型可以自定义特殊的索引获取方式

转载于:https://www.cnblogs.com/gui-lin/p/6526585.html

你可能感兴趣的文章
linux 学习之路(学linux必看)
查看>>
mavn项目(springMVC) 引入静态资源(js、css)等
查看>>
webservice异常
查看>>
开源 java CMS - FreeCMS2.2 敏感词管理
查看>>
域scope 介绍,及查找数据
查看>>
CodeForces 550E Brackets in Implications(构造)
查看>>
uva 10641 (来当雷锋的这回....)
查看>>
go-import下划线的作用
查看>>
Flink – Stream Task执行过程
查看>>
机器学习第1课:引言(Introduction)
查看>>
Echarts柱状图的点击事件
查看>>
shutil模块,ZipFile 和 TarFile 两个模块
查看>>
快速排序,一个爱情故事-java版
查看>>
iOS 输入时键盘处理问题
查看>>
USER Management | Role Categories | Roles | Indirect Responsibilities
查看>>
jquery修改a标签的href链接和文字
查看>>
PHP 使用 GeoLiteCity 库解析 IP 为地理位置
查看>>
win7旗舰版显示不了文件扩展名
查看>>
springMVC--动态验证码实现
查看>>
linux scp 命令
查看>>