Python: PyQt QTreeview example - selection

前端 未结 4 1639
醉梦人生
醉梦人生 2020-12-29 14:00

I\'m using Python 2.7 and Qt designer and I\'m new to MVC: I have a View completed within Qt to give me a directory tree list, and the controller in place to run things. My

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 14:22

    In PyQt5 it can be done in this way:

    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_())
    

提交回复
热议问题