How to inherit a subclass from QAbstractItemView to support item editing ?
-
Hi!
I have created a subclass of QAbstractItemView,and use QFileSystemModel as data-model.
For the purpose of renaming file, I use setEditTriggers(SelectedClicked), but it did not work when i clicked an item which already selected.
What should I do? Thanks! -
Hi and welcome to devnet,
Can you show the code of your subclass ?
-
@SGaist
I wrote it in python:class LCListPanel(QtWidgets.QAbstractItemView):
def __init__(self, parent=None): super().__init__(parent) delegate = LCItemDelegate() self.setItemDelegate(delegate) self.setEditTriggers(self.SelectedClicked) ... def edit(self, index: QtCore.QModelIndex, trigger, event): return super().edit(index, trigger, event)
class LCItemDelegate(QtWidgets.QItemDelegate):
def __init__(self, parent=None): super().__init__(parent) ... def createEditor(self, parent: QtWidgets.QWidget, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex): return super().createEditor(parent, option, index) def setEditorData(self, editor: QtWidgets.QWidget, index: QtCore.QModelIndex): super().setEditorData(editor, index) def setModelData(self, editor: QtWidgets.QWidget, model: QtCore.QAbstractItemModel, index: QtCore.QModelIndex): super().setModelData(editor, model, index) def updateEditorGeometry(self, editor: QtWidgets.QWidget, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex): editor.setGeometry(option.rect) def editorEvent(self, event: QtCore.QEvent, model: QtCore.QAbstractItemModel, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex): return super().editorEvent(event, model, option, index)
-
Why are you subclassing QAbstractItemView ? Based on the class name, it seems that using a QListView would be enough.
-
@ridincal
Is yourcreateEditor()
being called? What does itssuper().createEditor(parent, option, index)
actually return?As a separate issue, I'm not positive about this but I think you're supposed to derive from
QStyledItemDelegate
rather thanQItemDeletegate
. At least that's what In have used. -
@JonB
The function createEditor() never been called when I kept on clicking items.I tried to use QListView directly instead of subclassing from it, but it still did not work anymore.
code:
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1600, 1000) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.listView = QtWidgets.QListView(self.centralwidget) self.listView.setGeometry(QtCore.QRect(160, 80, 681, 751)) self.listView.setObjectName("listView") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 18)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) self.model1 = QtWidgets.QFileSystemModel() self.model1.setRootPath("") self.model1.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot) self.listView.setModel(self.model1) self.listView.setEditTriggers(QtWidgets.QAbstractItemView.SelectedClicked)
-
@ridincal
OK, so I imagine this is because aQFileSystemModel
/QAbstractItemView
is not editable? E.g.https://doc.qt.io/qt-5/qabstractitemview.html#edit
Starts editing the item corresponding to the given index if it is editable.
I would guess one of these needs addressing:
- Make sure your underlying
QFileSystemModel
is editable (I think it is EDIT Ummm, no.... ). - Verify your view items have
item->setFlags(item->flags() | Qt::ItemIsEditable);
. - Test that https://doc.qt.io/qt-5/qabstractitemview.html#edit-1 works, independent of clicking anywhere.
If I were you, I'd try attaching an existing
QTreeView
to aQFileSystemModel
and see how that behaves. If that works and yours does not, review your subclass ofQAbstractItemView
?STOP PRESS
Here's why, I assume: https://doc.qt.io/qt-5/qfilesystemmodel.html#readOnly-propThis property holds whether the directory model allows writing to the file system
If this property is set to false, the directory model will allow renaming, copying and deleting of files and directories.
This property is true by default
?
- Make sure your underlying