再次,我在QFileSystemModel和Signal上遇到了一些问题。
我想要一个文件管理器。当您单击左侧面板时,右侧面板将显示其子文件夹,就像所有文件管理器一样。仅显示具有特定名称的文件夹。例如:“ folder.asset”,而不是“ folder”。
我的问题是我无法将点击数据从treeView正确传输到listView。
这是代码指令(完整尺寸:1513x955):
现在我知道QModelIndex仅适用于相同的文件模型。所以我用我的方式将QModelIndex从一个文件模型转移到另一个文件模型。
完整代码:
import sys
from PyQt5 import QtCore, QtWidgets, QtGui, uic
class amFileSystemModel(QtWidgets.QFileSystemModel):
def __init__(self):
super(amFileSystemModel,self).__init__()
self.libPath = "f:/tmp2"
self.setRootPath(self.libPath)
self.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Dirs)
class amGui(QtWidgets.QWidget):
def __init__(self):
super(amGui, self).__init__()
#load ui file
uic.loadUi("f:/tmp2/gui.ui", self)
# data model
self.treeModel = amFileSystemModel()
self.listModel = amFileSystemModel()
# filter
self.listModel.setNameFilters(['*.asset'])
self.listModel.setNameFilterDisables(False)
self.proxyModel = QtCore.QSortFilterProxyModel()
self.proxyModel.setFilterRegExp(u"^([^.]+)$")
self.proxyModel.setSourceModel(self.treeModel)
# window
self.treeView.setModel(self.proxyModel)
self.treeView.setColumnHidden(1,True)
self.treeView.setColumnHidden(2,True)
self.treeView.setColumnHidden(3,True)
self.treeView.setRootIndex(self.proxyModel.mapFromSource(self.treeModel.index(self.treeModel.libPath))) # Root Index is 'f:/tmp2'
self.listView.setModel(self.listModel)
index = self.listModel.index('f:/tmp2') #
self.listView.setRootIndex(index)
print(self.listModel.filePath(index))
#print result: 'f:/tmp2', correct.
# clicked connect to my slot function
self.treeView.clicked.connect(self.proxySignalToModel)
# my slot function (setp 1)
def proxySignalToModel(self, proxySignal):
#(setp 2)
sourceIndex = self.proxyModel.mapToSource(proxySignal)
#(setp 3)
sourceDir = self.treeModel.filePath(sourceIndex) #result(for example): 'f:/tmp2/_folder2'. correct.
#(setp 4)
index = self.listModel.index(sourceDir) #
print(self.listModel.filePath(index))
#print result is '' empty string?! why??
self.listView.setRootIndex(index)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainui = amGui()
mainui.show()
sys.exit(app.exec_())