使用QFileSystemModel展开QTreeView中的项目

时间:2019-06-22 05:54:59

标签: python python-3.x pyqt pyqt5

当用户单击按钮时,我需要转到某个目录。

用户在QInputDialog中输入路径(例如,在Linux上为/ mnt / data / Music),然后程序进入该目录,例如Midnight Commander中的Mc或Ranger中的cd,但是我不知道该怎么做。 ..我已经非常了解所有文档

main.py

import sys
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QMainWindow, QInputDialog
import exampleQTV

class PyMap(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.search.clicked.connect(self.searchAction)

    def searchAction(self):
        text, ok = QInputDialog.getText(self, "Go to", "Enter path")
        if ok:
            path = text.split("/")

        for item in path:
            if item != "":
                print("ITEM:", item)
                self.leftView.keyboardSearch(item)
                self.leftView.setExpanded(self.leftView.currentIndex(), True)
                print("currentIndex():",self.leftView.model.filePath(self.leftView.currentIndex()))
                child = self.leftView.currentIndex().child(0,0)
                print("child:", self.leftView.model.filePath(child))

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.leftView = exampleQTV.exampleQTV()
        self.leftView.setObjectName("leftView")
        self.gridLayout.addWidget(self.leftView, 1, 0, 1, 1)
        self.search = QtWidgets.QPushButton(self.centralwidget)
        self.search.setObjectName("select")
        self.gridLayout.addWidget(self.search, 0, 0, 1, 2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.search.setText(_translate("MainWindow", "Search"))


def main():
    app = QtWidgets.QApplication(sys.argv)
    window = PyMap()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()


exampleQTV.py

from PyQt5.QtWidgets import QTreeView, QFileSystemModel
from PyQt5.QtCore import QDir

class exampleQTV(QTreeView):
    def __init__(self):
        QTreeView.__init__(self)
        self.model = QFileSystemModel()
        self.model.setRootPath(QDir.rootPath())
        self.setModel(self.model)
        self.eventCalled = False
        self.requestForEscape = False

        self.setColumnHidden(1, True)
        self.setColumnHidden(2, True)
        self.setColumnHidden(3, True)
        self.expandAll()

1 个答案:

答案 0 :(得分:0)

如果您想扩展项目,则必须扩展祖先,在以下代码中,您可以实现该逻辑:

<iframe src="https://d1mlukbqb3dh9w.cloudfront.net/PortraitCourse/Module01/Module01Section1/story.html" height="655" width="100%"></iframe>