qt 展开指定目录_如何展开指定目录?

在下面的代码中如何展开指定目录,在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_())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值