BoshWash
BoshWash

Reputation: 5440

Can't edit items in QTreeView with QFileSystems

I'm trying to get a QTreeView to allow the user to edit file names from a QFileSystemModel. However Qt just prints:

edit: editing failed

I get the same result with PySide and PyQt.

Opening the editor with openPersistenEditor() works, but I would prefer to use the build-in mechanism.

import sys
from PyQt4.QtGui import QTreeView, QFileSystemModel, QApplication


class TestView(QTreeView):
    def __init__(self, directory, *args, **kwargs):
        super(TestView, self).__init__(*args, **kwargs)

        self.file_system_model = QFileSystemModel()

        self.file_system_model.setRootPath(directory)
        index = self.file_system_model.index(directory)
        self.setModel(self.file_system_model)
        self.setRootIndex(index)
        self.activated.connect(self._on_edit)

    def _on_edit(self, index):
        # self.closePersistentEditor(index)
        # app.processEvents()
        self.edit(self.currentIndex())
        # self.openPersistentEditor(index)


if __name__ == '__main__':
    app = QApplication([])
    directory = r'c:/'
    dialog = TestView(directory)

    dialog.show()
    sys.exit(app.exec_())

Upvotes: 2

Views: 423

Answers (1)

ekhumoro
ekhumoro

Reputation: 120608

The model is read-only by default, so you need to add:

    self.file_system_model.setReadOnly(False)

Upvotes: 2

Related Questions