第五十六课、模型视图设计模式(中)------------------狄泰软件学院

一、模型视图设计模式

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 <QtGui/QMainWindow>
#include <QFileSystemModel>
#include <QPlainTextEdit>

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 <QByteArray>
#include <QBuffer>
#include <QDir>
#include <QTextStream>
#include <QModelIndex>
#include <QIODevice>

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<m_fileModel.rowCount(root); i++)//Returns the number of rows under the given parent.
        {
            QModelIndex ci = m_fileModel.index(i, 0, root);//获取第i行第0列数据的索引

            out << ci.data().toString() << endl;
        }

        out.flush();
        buffer.close();

        if(buffer.open(QIODevice::ReadOnly))
        {
            QTextStream in(&buffer);

            m_edit.appendPlainText(in.readAll());

            buffer.close();
        }

    }
}

MainWindow::~MainWindow()
{
    
}
MainWindow.cpp
#include <QtGui/QApplication>
#include "MainWindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}
main.cpp
//输出结果

1
lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____
lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____
0x28fe4c
0x28fe4c
F:/Qt/lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____
lesson56-build-desktop-Qt_4_7_4___PATH__4_7_4____

debug
Makefile
Makefile.Debug
Makefile.Release
release

二、小结

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

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

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

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

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

posted @ 2017-03-09 22:28  lgc202  阅读(526)  评论(0编辑  收藏  举报