如何展开指定目录?

在下面的代码中如何展开指定目录,在total commander中有类似的命令"cd d:\aaa\py3\abc\",函数cdpath该如何实现,无关目录尽量不展开,就是一步到达某一目录

import sys
import os
from PyQt5 import QtCore
from PyQt5.Qt import *

class MainWidget(QWidget):

def __init__(self, parent=None):
    super(MainWidget, self).__init__(parent)

    #获取系统所有文件
    self.model01 = QFileSystemModel()
    #进行筛选只显示文件夹,不显示文件和特色文件
    self.model01.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
    self.model01.setRootPath('')

    #定义创建左边窗口
    self.treeView1 = QTreeView(self)
    self.treeView1.setModel(self.model01)
    for col in range(1, 4):
        self.treeView1.setColumnHidden(col, True)
    self.treeView1.doubleClicked.connect(self.initUI)

    #定义创建右边窗口
    self.model02 = QStandardItemModel()
    self.treeView2 = QTreeView(self)
    self.treeView2.setModel(self.model02)
    self.treeView2.clicked.connect(self.itemClicked)

    #将创建的窗口进行添加
    self.layout = QHBoxLayout()
    self.layout.addWidget(self.treeView1)
    self.layout.addWidget(self.treeView2)
    self.setLayout(self.layout)

def itemClicked(self, Qmodelidx):
    text = self.model02.data(Qmodelidx)
    print(text)
    indexItem = self.model02.index(Qmodelidx.row(), 0, Qmodelidx.parent())
    #print(indexItem)
    fileName = self.model02.data(indexItem)
    print(fileName)
    filePath = self.model01.filePath(self.treeView1.currentIndex())
    #filePath = self.model01.currentItem()
    #index=self.model01.currentIndex()
    print(filePath)
    '''
    #每次点击右边窗口数据
    filePath = self.model02.filePath(Qmodelidx)
    '''
    
def initUI(self, Qmodelidx):
    #每次点击清空右边窗口数据
    self.model02.clear()
    #定义一个数组存储路径下的所有文件
    PathData = []
    #获取双击后的指定路径
    filePath = self.model01.filePath(Qmodelidx)
    # List窗口文件赋值
    PathDataName = self.model02.invisibleRootItem()
    #拿到文件夹下的所有文件
    PathDataSet = os.listdir(filePath)
    #进行将拿到的数据进行排序
    PathDataSet.sort()
    #遍历判断拿到的文件是文件夹还是文件,Flase为文件,True为文件夹
    for Data in range(len(PathDataSet)):
        if os.path.isdir(filePath + '\\' + PathDataSet[Data]) == False:
            PathData.append(PathDataSet[Data])
        elif os.path.isdir(filePath + '\\' + PathDataSet[Data]) == True:
            #print('2')
            pass
    #将拿到的所有文件放到数组中进行右边窗口赋值。
    for got in range(len(PathData)):
        gosData = QStandardItem(PathData[got])
        PathDataName.setChild(got, gosData)

def cdpath(self, path):
    if os.path.exists(path):
        pass

if name == "__main__":

app = QApplication(sys.argv)
window = MainWidget()
window.resize(600, 400)
window.show()
sys.exit(app.exec_())

cdpath点击两次可以了
阅读 1.8k
1 个回答
def cdpath(self, path):
    if os.path.exists(path):
        #pass
        index = self.model01.index(str(path))
        print(path,index,self.model01.filePath(index))
        #self.treeView1.setRootIndex(self.model01.index(os.path.dirname(str(path))))
        self.treeView1.scrollTo(index)
        self.treeView1.setCurrentIndex(index)
        self.initUI(index)
        已经部分达到目的,但是左侧窗口没有展开,也没有设置到正确的item,我希望保留其他目录的显示,所以没有使用self.treeView1.setRootIndex(self.model01.index(os.path.dirname(str(path)))),如何是好?
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题