码迷,mamicode.com
首页 > 其他好文 > 详细

t model/view 架构自定义模型之QFileSystemModel

时间:2014-08-12 16:10:44      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   文件   数据   for   2014   ar   

# -*- coding: utf-8 -*-

# python:2.x

#QFileSystemModel

"""

Qt  内置了两种模型:QStandardItemModel 和

QFileSystemModel 。QStandardItemModel 是一种多用途的模型,能够让列表、表格、

树等视图显示不同的数据结构。这种模型会将数据保存起来。试想一下, 列表和表格所要求

的数据结构肯定是不一样的:前者是一维的,后者是二维的。因此,模型需要保存有实际数

据,当视图是列表时,以一维的形式提供数据;当视图是表格时,以二维的形式提供数据。

QFileSystemModel 则是另外一种方式。它的作用是维护一个目录的信息。因此,它不需

要保存数据本身,而是保存这些在本地文件系统中的实际数据的一个索引。我们可以利用

QFileSystemModel 显示文件系统的信息、甚至通过模型来修改文件系统

"""

#QFileSystemModel会把根目录路径设置为当前目录QFileSystemModel 完全将所能想到的东西——名称、大小、类型、修改时间等全部显示出来,可见其强大之处

__author__ = ‘Administrator‘

#如果你要立即刷新结果,需要通知QFileSystemWatcher 类。

from PyQt4.QtGui import  *

from PyQt4.Qt import *

from PyQt4 import QtGui, QtCore

from PyQt4.QtCore import *

import sys

class Painterd(QWidget):

    def __init__(self):

        super(Painterd,self).__init__()

        self.setFixedSize(300,200)

        self.vector()

    def vector(self):

        self.model=QFileSystemModel()

        self.model.setRootPath(QDir.currentPath())

        self.treeview=QTreeView(self)

        self.treeview.setModel(self.model)

        self.treeview.setRootIndex(self.model.index(QDir.currentPath()))

        mkdirpubutton=QPushButton(‘mkdir‘,self)

        self.rmbutton=QPushButton(‘rmbu‘,self)

        self.buttonlayout=QVBoxLayout()

        self.buttonlayout.addWidget(mkdirpubutton)

        self.buttonlayout.addWidget(self.rmbutton)

        layout=QVBoxLayout()

        layout.addWidget(self.treeview)

        layout.addLayout(self.buttonlayout)

        self.setLayout(layout)

        self.setWindowTitle("File System Model")

        mkdirpubutton.clicked.connect(self.mkdir)

        self.rmbutton.clicked.connect(self.rm)

    def rm(self):

        index=self.treeview.currentIndex()

        if not index.isValid():#判断是没有目录被选择的

            return

        ok=False

        if self.model.fileInfo(index).isDir():#目录和文件的删除不是一个函数,需要调用 isDir()函数检测

            ok=self.model.rmdir(index)

        else:

            ok=self.model.remove(index)

        if not ok:

            QMessageBox.information(self,‘remove‘,self.tr(‘remove%1‘).arg(self.model.fileName(index)))

    def mkdir(self):

        index=self.treeview.currentIndex()

        if not index.isValid():#判断是没有目录被选择的

            return

        ok,dirname=QInputDialog.getText(self,‘create dir‘,‘dir name‘)

        print dir(ok)

        if not ok.isEmpty():

            if not self.model.mkdir(index,ok).isValid():

                QMessageBox.information(self,‘create‘,‘the dir‘)

def main():

    app = QtGui.QApplication(sys.argv)

    ex =Painterd()

    ex.show()

    sys.exit(app.exec_())

if __name__ == ‘__main__‘:

    main()

如图:bubuko.com,布布扣

bubuko.com,布布扣

#更多请看:http://devbean.blog.51cto.com/448512/265658/

t model/view 架构自定义模型之QFileSystemModel,布布扣,bubuko.com

t model/view 架构自定义模型之QFileSystemModel

标签:blog   http   io   文件   数据   for   2014   ar   

原文地址:http://www.cnblogs.com/mhxy13867806343/p/3906994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!