SHARE
TWEET

Untitled

a guest Dec 6th, 2018 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. from PyQt5.QtWidgets import (QApplication, QWidget, QTreeView, QFileSystemModel, QVBoxLayout, QMenu, QAction)
  3. from PyQt5.QtCore import QDir
  4. import os
  5.  
  6. class MyTreeWidget(QWidget):
  7.     def __init__(self, parent=None):
  8.         super().__init__(parent)
  9.         self.setWindowTitle('Direct tree')
  10.         self.resize(300, 400)
  11.         self.vbox = QVBoxLayout()
  12.         self.model = QFileSystemModel()
  13.         self.model.setRootPath(QDir.rootPath())
  14.         self.tree = QTreeView()
  15.         self.tree.setModel(self.model)
  16.         self.tree.setRootIndex(self.model.index(os.getcwd()))
  17.         self.vbox.addWidget(self.tree)
  18.         self.setLayout(self.vbox)
  19.         self.vbox.setContentsMargins(0, 0, 0, 0)
  20.  
  21.         self.tree.setAnimated(False)
  22.         self.tree.setIndentation(20)
  23.         self.tree.setSortingEnabled(True)
  24.  
  25.     def contextMenuEvent(self, e):
  26.         contextMenu = QMenu(self)
  27.         self.filepath = QAction('Get file path')
  28.         self.filepath.triggered.connect(self.on_filepath)
  29.         contextMenu.addAction(self.filepath)
  30.         contextMenu.exec_(self.mapToGlobal(e.pos()))
  31.  
  32.     def on_filepath(self):
  33.         pass
  34.  
  35. if __name__ == "__main__":
  36.     app = QApplication(sys.argv)
  37.     win = MyTreeWidget()
  38.     win.show()
  39.     sys.exit(app.exec_())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top