Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to inherit a subclass from QAbstractItemView to support item editing ?

How to inherit a subclass from QAbstractItemView to support item editing ?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ridincal
    wrote on last edited by
    #1

    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!

    JonBJ 1 Reply Last reply
    0
    • R ridincal

      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!

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ridincal
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          Can you show the code of your subclass ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi and welcome to devnet,

            Can you show the code of your subclass ?

            R Offline
            R Offline
            ridincal
            wrote on last edited by
            #5

            @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)
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply
              0
              • SGaistS SGaist

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

                R Offline
                R Offline
                ridincal
                wrote on last edited by
                #7

                @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?

                JonBJ 1 Reply Last reply
                0
                • R ridincal

                  @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?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #8

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

                  R 1 Reply Last reply
                  0
                  • JonBJ JonB

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

                    R Offline
                    R Offline
                    ridincal
                    wrote on last edited by ridincal
                    #9

                    @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)
                    
                    JonBJ 1 Reply Last reply
                    0
                    • R ridincal

                      @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)
                      
                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #10

                      @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

                      ?

                      R 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @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

                        ?

                        R Offline
                        R Offline
                        ridincal
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved