Please help me understand the next problem.

There is an application where the QTreeView widget with the tree structure of files is located on the left and the QTextEdit text field on the right.

How to make it so that if you double-click with the left mouse button on, say, a text file, it would open a text field?

In other words, I need to get the absolute path to the file by double clicking on it. Tell me how to do it.

import sys from PyQt5.QtWidgets import (QApplication, QWidget, QSplitter, QTreeView, QTextEdit, QFileSystemModel, QVBoxLayout) from PyQt5.QtCore import QDir import os class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle('Direct tree') self.resize(600, 400) self.vbox = QVBoxLayout() self.splitter = QSplitter() 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.textEdit = QTextEdit() self.splitter.addWidget(self.tree) self.splitter.addWidget(self.textEdit) self.splitter.setSizes([50, 200]) self.vbox.addWidget(self.splitter) self.setLayout(self.vbox) self.tree.setAnimated(False) self.tree.setIndentation(20) self.tree.setSortingEnabled(True) if __name__ == "__main__": app = QApplication(sys.argv) win = MyWidget() win.show() sys.exit(app.exec_()) 

enter image description here

    1 answer 1

    Actions:

    • Add doubleClicked signal to handle double click: self.tree.doubleClicked.connect(self._on_double_clicked)
    • Add signal processing method:

       def _on_double_clicked(self, index): file_name = self.model.filePath(index) with open(file_name, encoding='utf-8') as f: text = f.read() self.textEdit.setPlainText(text) 

    The doubleClicked signal sends the element index , and to get the path by the index, you need to call the filePath method on the model .


    Full example:

     from PyQt5.Qt import ( QApplication, QWidget, QSplitter, QTreeView, QTextEdit, QFileSystemModel, QVBoxLayout, QDir ) import os class MyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) self.setWindowTitle('Direct tree') 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.tree.doubleClicked.connect(self._on_double_clicked) self.tree.setAnimated(False) self.tree.setIndentation(20) self.tree.setSortingEnabled(True) self.textEdit = QTextEdit() splitter = QSplitter() splitter.addWidget(self.tree) splitter.addWidget(self.textEdit) splitter.setSizes([50, 200]) main_layout = QVBoxLayout() main_layout.addWidget(splitter) self.setLayout(main_layout) def _on_double_clicked(self, index): file_name = self.model.filePath(index) with open(file_name, encoding='utf-8') as f: text = f.read() self.textEdit.setPlainText(text) if __name__ == "__main__": app = QApplication([]) win = MyWidget() win.resize(600, 400) win.show() app.exec() 

    Screenshot:

    enter image description here