簡體   English   中英

SetRootPath不能按預期設置工作

[英]SetRootPath doesn't set work as expected

我已經從這篇文章中使用了部分代碼(PyQt5)

from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication

class Main(QTreeView):
    def __init__(self):
        QTreeView.__init__(self)
        model = QFileSystemModel()
        model.setRootPath('C:\\')
        self.setModel(model)
        self.doubleClicked.connect(self.test)

    def test(self, signal):
        file_path=self.model().filePath(signal)
        print(file_path)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Main()
    w.show()
    sys.exit(app.exec_())

我對這條線有疑問

model.setRootPath( 'C:\\')

當我運行程序時,它始終顯示驅動器,例如C:D:只是不顯示C:\\的內容,或者即使我鍵入“ C:\\ Users \\”或什至不存在的路徑,它也始終會顯示,請參見所附圖片,我在做什么錯?

PyQt程序的圖像顯示了文件管理器

我正在使用:Windows 10,PyCharm,Python 3.5,PyQt5,

謝謝你的幫助。

您必須使用setRootIndex()QTreeView指示您的根項目是什么:

from PyQt5.QtCore import QDir

from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication

class Main(QTreeView):
    def __init__(self):
        QTreeView.__init__(self)
        model = QFileSystemModel()
        self.setModel(model)
        model.setRootPath(QDir.rootPath())
        self.setRootIndex(model.index("C:"))
        self.doubleClicked.connect(self.test)

    def test(self, signal):
        file_path=self.model().filePath(signal)
        print(file_path)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Main()
    w.show()
    sys.exit(app.exec_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM