Important: Please read the Qt Code of Conduct -

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!

  • @ridincal
    Start by trying with QAbstractItemView::AllEditTriggers? Does anything work, or is it only SelectedClicked which is not behaving?

  • I already tried QAbstractItemView::AllEditTriggers,nothing happened...

  • Lifetime Qt Champion

    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):
        delegate = LCItemDelegate()
    def edit(self, index: QtCore.QModelIndex, trigger, event):
        return super().edit(index, trigger, event)

    class LCItemDelegate(QtWidgets.QItemDelegate):

    def __init__(self, parent=None):
    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):
    def editorEvent(self, event: QtCore.QEvent, model: QtCore.QAbstractItemModel, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex):
        return super().editorEvent(event, model, option, index)

  • Lifetime Qt Champion

    Why are you subclassing QAbstractItemView ? Based on the class name, it seems that using a QListView would be enough.

  • @SGaist
    Yes. I change the base class to QListView, it still can not enter the edit mode. why?
    What is the standard procedure to make the item editable?

  • @ridincal
    Is your createEditor() being called? What does its super().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 than QItemDeletegate. 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.


    from PyQt5 import QtCore, QtGui, QtWidgets
    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
            MainWindow.resize(1600, 1000)
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.listView = QtWidgets.QListView(self.centralwidget)
            self.listView.setGeometry(QtCore.QRect(160, 80, 681, 751))
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 18))
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.model1 = QtWidgets.QFileSystemModel()
            self.model1.setFilter(QtCore.QDir.AllDirs | QtCore.QDir.NoDotAndDotDot)

  • @ridincal
    OK, so I imagine this is because a QFileSystemModel/QAbstractItemView is not editable? E.g.

    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 works, independent of clicking anywhere.

    If I were you, I'd try attaching an existing QTreeView to a QFileSystemModel and see how that behaves. If that works and yours does not, review your subclass of QAbstractItemView?

    Here's why, I assume:

    This 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


  • @JonB
    Thanks a lot!
    When I set the property isReadOnly to False, createEditor() has been called!

Log in to reply