Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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):
        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)

  • 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.

    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 a QFileSystemModel/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 a QFileSystemModel and see how that behaves. If that works and yours does not, review your subclass of QAbstractItemView?

    STOP PRESS
    Here's why, I assume: https://doc.qt.io/qt-5/qfilesystemmodel.html#readOnly-prop

    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