Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from PyQt5.QtWidgets import (QApplication, QWidget, QTreeView, QFileSystemModel, QVBoxLayout, QMenu, QAction)
- from PyQt5.QtCore import QDir
- import os
- class MyTreeWidget(QWidget):
- def __init__(self, parent=None):
- super().__init__(parent)
- self.setWindowTitle('Direct tree')
- self.resize(300, 400)
- self.vbox = QVBoxLayout()
- self.model = QFileSystemModel()
- self.model.setRootPath(QDir.rootPath())
- self.tree = QTreeView()
- self.tree.setModel(self.model)
- self.tree.setRootIndex(self.model.index(os.getcwd()))
- self.vbox.addWidget(self.tree)
- self.setLayout(self.vbox)
- self.vbox.setContentsMargins(0, 0, 0, 0)
- self.tree.setAnimated(False)
- self.tree.setIndentation(20)
- self.tree.setSortingEnabled(True)
- def contextMenuEvent(self, e):
- contextMenu = QMenu(self)
- self.filepath = QAction('Get file path')
- self.filepath.triggered.connect(self.on_filepath)
- contextMenu.addAction(self.filepath)
- contextMenu.exec_(self.mapToGlobal(e.pos()))
- def on_filepath(self):
- pass
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- win = MyTreeWidget()
- win.show()
- sys.exit(app.exec_())
Add Comment
Please, Sign In to add comment