【PyQt4 实例24】文件浏览器

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))

class FileViewer(QMainWindow):
    def __init__(self,parent=None):
        super(FileViewer,self).__init__(parent)
        self.setWindowTitle(self.tr("文件浏览器"))
        
        mainWidget = QWidget()
        mainLayout=QGridLayout()
        self.LineEditDir = QLineEdit(self)
        self.ListWidgetFile = QListWidget(self)
        mainLayout.addWidget(self.LineEditDir,0,0)
        mainLayout.addWidget(self.ListWidgetFile,1,0)
        mainWidget.setLayout(mainLayout)
        self.setCentralWidget(mainWidget)
        self.setMouseTracking(True)       
        self.ListWidgetFile.itemDoubleClicked.connect(self.slotShowDir)

    def showFileInfoList(self,list):
        self.ListWidgetFile.clear()
        for i in xrange(len(list)):
            tmpFileInfo = list[i]
            icon = QIcon("./image/dir.png") if tmpFileInfo.isDir() else QIcon("./image/file.png")
            fileName = tmpFileInfo.fileName()
            tmp = QListWidgetItem(icon,fileName)
            self.ListWidgetFile.addItem(tmp)
                
    def slotShow(self,dir1):
        string = QString()
        string < "*"
        list = dir1.entryInfoList(string,QDir.AllEntries,QDir.DirsFirst)
        self.showFileInfoList(list)
    
    def slotShowDir(self):
        item = self.ListWidgetFile.currentItem()
        str = item.text()
        dir = QDir()
        dir.setPath(self.LineEditDir.text())
        dir.cd(str)
        self.LineEditDir.setText(dir.absolutePath())
        self.slotShow(dir)
    
    def keyPressEvent(self,event):
        if event.key() == Qt.Key_Return:
            dir = QDir()
            dir.setPath(self.LineEditDir.text())   
            self.LineEditDir.setText(dir.absolutePath())         
            self.slotShow(dir)

app=QApplication(sys.argv)
dialog=FileViewer()
dialog.show()
app.exec_()


你可能感兴趣的:(python)