Issue
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_())
Solution
The model is read-only by default, so you need to add:
self.file_system_model.setReadOnly(False)
Answered By - ekhumoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.