programming python qt




# !/usr/bin/env python
# coding: utf-8

import re import operator import os import sys import stat from PyQt4.QtCore import * from PyQt4.QtGui import *

class File(object):


‘type’, ‘name’, ‘size’, ‘mtime’ ]
def __init__(self, path):

self.path=os.path.abspath(path)[None, None, None, None] s=os.stat(path) mode=s[stat.ST_MODE] if stat.S_ISDIR(mode):[0]=‘dir’ elif stat.S_ISLNK(mode):[0]=‘link’ elif stat.S_ISREG(mode):[0]=‘file’ else:[0]=‘unknown’[1]=os.path.basename(path)[2]=s[stat.ST_SIZE][3]=s[stat.ST_MTIME]

def __str__(self):

return “<%s %s>” % ([0],[1])

def __getitem__(self, key):


def is_dir(self):


class MyTableModel(QAbstractTableModel):

def __init__(self, parent, path):

super(MyTableModel, self).__init__(parent) def get_stat(name): f=File(os.path.join(path, name)) return f[get_stat(os.path.join(path, “..”))]+[get_stat(e) for e in os.listdir(path)]
def rowCount(self, parent):

return len(

def columnCount(self, parent):

return len(File.header)

def data(self, index, role):

if not index.isValid():

return None
elif role != Qt.DisplayRole:

return None


def headerData(self, col, orientation, role):

if orientation == Qt.Horizontal and role == Qt.DisplayRole:

return File.header[col]

return None

def sort(self, Ncol, order):

”““Sort table by given column number. “”” self.emit(SIGNAL(“layoutAboutToBeChanged()”)) = sorted(, key=operator.itemgetter(Ncol), reverse=(order!=Qt.DescendingOrder)) self.emit(SIGNAL(“layoutChanged()”))

def getRow(self, index):


class MyWindow(QWidget):

def __init__(self, *args): QWidget.__init__(self,*args)

# create the view = QTableView() # hide grid # hide vertical header vh = vh.setVisible(False) # set horizontal header properties hh = hh.setStretchLastSection(True) # set column width to fit contents # enable sorting # layout layout = QVBoxLayout() layout.addWidget( self.setLayout(layout)

# double click

def chdir(self, path): = MyTableModel(self, path) # set the table model
def onDoubleClick(self, event): if not item.is_dir(): return self.chdir(item.path)

def main():

app = QApplication(sys.argv) w = MyWindow() w.chdir(“.”) sys.exit(app.exec_())

if __name__ == “__main__“:



  • 種類毎のアイコン表示
  • 日付の表示
  • 数字カラムの右寄せ
  • 行選択
  • FilterProxyModel