将嵌套列表转换为QTree窗口小部件

时间:2018-03-04 20:57:12

标签: python pyqt pyqt5 qtreewidget

我有一个文件和目录列表,我现在需要将其构建到树结构中,但是我在将它们添加到树中时遇到了问题。

实施例: 目录列表

directory=[["$Recycle.Bin","S-1-5-18"],["AutoDesk",["AutoCad","File1","File2","File3"],["Inventor","project1"],["WI","Copyright"],"Frank"]]

设置Tree Widget:

    self.tab1.layout = QGridLayout(self)
    self.tree=QTreeWidget()
    self.tree.setAnimated(False)
    self.tree.setIndentation(20)
    self.tree.setSortingEnabled(True)
    self.tree.setWindowTitle("dirview")
    self.tree.headerItem().setText(0,"Directory")
    self.tree.headerItem().setText(1,"Size")
    self.tree.headerItem().setText(2,"Path")
    self.tree.headerItem().setText(3,"Modified")
    self.tree.headerItem().setText(4,"Accessed")
    self.tree.headerItem().setText(5,"Created")
    self.num_directories=0
    self.directory_parser(directory,self.num_directories)

目录解析器功能:

def directory_parser(self, directory,num_directories):
    for directories in directory:
        if isinstance(directories,list):
            Gui_Directory=directories.pop(0)
            self.tree.insertTopLevelItems(num_directories,"text")
            self.directory_parser(directories, num_directories)
            num_directories=num_directories+1
        else:
            File_name=directories
            print(File_name)

输出应该如下:

  $Recycle.Bin
       ->S-1-5-18
  AutoDesk
    ->AutoCad
          ->File1
          ->File2
          ->File3
     -> Inventor
          ->project1
      -> WI
          ->Copyright
  Frank

0 个答案:

没有答案