Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

漏网之鱼,QFileSystemModel中已经设置setFilter( QDir.Files),还是极少数目录被显示

漏网之鱼,QFileSystemModel中已经设置setFilter( QDir.Files),还是极少数目录被显示
image.png
image.png
其它目录正常image.png
如果改成QListView(),没有日期等信息显示,纠结中,请高人指教!

import sys
import os

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Widget(QWidget):

def __init__(self, *args, **kwargs):
    QWidget.__init__(self, *args, **kwargs)
    hlay = QHBoxLayout(self)
    self.treeview = QTreeView()
    self.listview = QTreeView()
    hlay.addWidget(self.treeview)
    hlay.addWidget(self.listview)
    #self.listview = QListView()#没有日期等信息
    
    #path = QDir.rootPath()
    path = ''

    self.dirModel = QFileSystemModel()
    #self.dirModel.setRootPath(QDir.rootPath())
    self.dirModel.setRootPath('')
    self.dirModel.setFilter(QDir.NoDotAndDotDot | QDir.AllDirs)

    self.fileModel = QFileSystemModel()
    #self.fileModel.setFilter(QDir.NoDotAndDotDot |  QDir.Files)
    self.fileModel.setFilter( QDir.Files)

    self.treeview.setModel(self.dirModel)
    self.listview.setModel(self.fileModel)
    self.listview.setSelectionMode(QAbstractItemView.ExtendedSelection)
    self.treeview.setColumnHidden(1, True)
    self.treeview.setColumnHidden(2, True)
    self.treeview.setColumnWidth(0, 500)
    self.listview.setColumnWidth(0, 500)

    self.treeview.setRootIndex(self.dirModel.index(path))
    self.listview.setRootIndex(self.fileModel.index(path))

    self.treeview.clicked.connect(self.on_clicked)
    self.listview.clicked.connect(self.itemClicked)

def on_clicked(self, index):
    path = self.dirModel.fileInfo(index).absoluteFilePath()
    self.listview.setRootIndex(self.fileModel.setRootPath(path))
def itemClicked(self, Qmodelidx):
    text = self.fileModel.data(Qmodelidx)
    print(text)
    indexItem = self.fileModel.index(Qmodelidx.row(), 0, Qmodelidx.parent())
    #print(indexItem)
    fileName = self.fileModel.data(indexItem)
    print(fileName)
    print(self.dirModel.filePath(Qmodelidx))

if name == '__main__':

app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...