pyqt5sizepyside2qtablewidgetqstyleditemdelegate

How to resize QTableWidgetItem to its editor size then to its text size using QStyledItemDelegate?


I created my own CustomDelegate class derived from QStyledItemDelegate:

class CustomDelegate(QStyledItemDelegate):
    def __init__(self, parent):
        super().__init__(parent)

    def createEditor(self, parent, option, index):
        editor = QWidget(parent)
        editor_hlayout = QHBoxLayout(editor)
        button = QPushButton()
        line_edit = QLineEdit()
        editor_hlayout.addWidget(button)
        editor_hlayout.addWidget(line_edit)
        return editor

    def setEditorData(self, editor, index):
        model_data = index.model().data(index, Qt.EditRole)
        editor.layout().itemAt(1).widget().setText(model_data) # Set line_edit value

    def setModelData(self, editor, model, index):
        editor_data =  editor.layout().itemAt(1).widget().text() # Get line_edit value
        model.setData(index, editor_data, Qt.EditRole)
        
    def updateEditorGeometry(self, editor, option, index):
        editor.setGeometry(option.rect)

I set it for the last column of the QTableWidget my_table :

my_delegate = CustomDelegate(my_window)
my_table.setItemDelegateForColumn(my_table.columnCount()-1, my_delegate)

To be precise, my goal is to edit the table widget item size when double-clicking it so that it fits the editor size and properly displays it, then edit the table widget item size right after exiting editor mode so that it fits its text size back again.

To that end, I added the lines index.model().setData(index, editor.sizeHint(), Qt.SizeHintRole) in createEditor method and model.setData(index, QTableWidgetItem(editor_data).sizeHint(), Qt.SizeHintRole) in setModelData method.

The problem is that QTableWidgetItem(editor_data).sizeHint() returns (-1, -1) size. I also tried with QTextDocument(editor_data).size() but it does not fit the text width (it is slightly smaller).


Solution

  • The editor of a item view should not change the size of its index. The only cases for which this is considered valid is for persistent editors and index widgets, but due to their nature it makes sense: they are expected to persist on the view, and not only it's acceptable that they require the view to eventually expand their row or column, but also necessary in order to avoid editors hiding other items (or editors).

    Any change in the section sizes can be potentially very demanding to the view, especially if it has lots of data and any of the header uses the ResizeToContents mode, that's why the default factory editors (QLineEdit, QDate/TimeEdit and Q[Double]SpinBox) don't update the index sizes but eventually extend their geometry temporarily.

    I would suggest to follow this practice, and eventually update the geometry in updateEditorGeometry() according to the editor position.

    In order to optimize the available space, you could use some precautions:

    Also note that:

        def createEditor(self, parent, option, index):
            editor = QWidget(parent)
            editor.setAutoFillBackground(True)
            editor_hlayout = QHBoxLayout(editor)
            editor_hlayout.setContentsMargins(0, 0, 0, 0)
            editor_hlayout.setSpacing(1)
            button = QToolButton()
            editor.line_edit = QLineEdit(frame=False)
            editor_hlayout.addWidget(button)
            editor_hlayout.addWidget(editor.line_edit)
            editor.setFocusProxy(editor.line_edit)
    
            # eventually (see note)
            editor.setObjectName('delegateEditor')
            editor.setStyleSheet('''
                #delegateEditor {
                    border: 1px solid palette(mid); 
                    background: palette(base);
                }
            ''')
            return editor
    
        def setEditorData(self, editor, index):
            model_data = index.model().data(index, Qt.EditRole)
            editor.line_edit.setText(model_data)
    
        def setModelData(self, editor, model, index):
            editor_data =  editor.line_edit.text()
            model.setData(index, editor_data, Qt.EditRole)
    
        def updateEditorGeometry(self, editor, option, index):
            super().updateEditorGeometry(editor, option, index)
            editor.resize(editor.sizeHint().width(), editor.height())
            rect = editor.geometry()
            parentRect = editor.parent().rect()
            if not parentRect.contains(rect):
                if rect.right() > parentRect.right():
                    rect.moveRight(parentRect.right())
                if rect.x() < parentRect.x():
                    rect.moveLeft(parentRect.x())
                if rect.bottom() > parentRect.bottom():
                    rect.moveBottom(parentRect.bottom())
                if rect.y() < parentRect.y():
                    rect.moveTop(parentRect.y())
                editor.setGeometry(rect)
    

    Note: if you do use stylesheets, it's possible that the line edit will partially draw over the border; in that case, use editor_hlayout.setContentsMargins(0, 0, 1, 0).


    That said, if you really want to update the view sizes, you still can, but it can be a bit tricky.

    The trick is to keep track of the editor and its index, manually resize the sections in order to fit its size, and then restore those sizes when the editor is destroyed.

    class CustomDelegate(QStyledItemDelegate):
        def __init__(self, parent):
            self.editorData = {}
            super().__init__(parent)
    
        def createEditor(self, parent, option, index):
            editor = QWidget(parent)
            editor.setAutoFillBackground(True)
            editor_hlayout = QHBoxLayout(editor)
            editor_hlayout.setContentsMargins(0, 0, 0, 0)
            editor_hlayout.setSpacing(1)
            button = QToolButton()
            editor.line_edit = QLineEdit(frame=False)
            editor_hlayout.addWidget(button)
            editor_hlayout.addWidget(editor.line_edit)
            editor.setFocusProxy(editor.line_edit)
            view = option.widget
            # store the editor, the view and also the current sizes
            self.editorData[index] = (editor, view, 
                view.horizontalHeader().sectionSize(index.column()), 
                view.verticalHeader().sectionSize(index.row())
                )
            # THEN, resize the row and column, which will call sizeHint()
            view.resizeColumnToContents(index.column())
            view.resizeRowToContents(index.row())
            # delay a forced scroll to the index to ensure that the editor is
            # visible after the sections have been resized
            QTimer.singleShot(1, lambda: view.scrollTo(index))
            return editor
    
        def sizeHint(self, opt, index):
            if index in self.editorData:
                # sizeHint doesn't provide access to the editor
                editor, *_ = self.editorData[index]
                return editor.sizeHint()
            return super().sizeHint(opt, index)
    
        def destroyEditor(self, editor, index):
            super().destroyEditor(editor, index)
            if index in self.editorData:
                editor, view, width, height = self.editorData.pop(index)
                view.horizontalHeader().resizeSection(index.column(), width)
                view.verticalHeader().resizeSection(index.row(), height)
    

    Note that if the index is near the edge (last row or column), when the editor is destroyed it's possible that the view won't properly adjust its scroll bars if the scroll mode is ScrollPerItem. AFAIK there's no easy workaround for this.

    Remember what said above, though. As Ian Malcolm would say, whether this could be done or not, you should stop and think if you should.

    Update about focus issues

    As pointed out in comments, if the user clicks somewhere else when the editor is active, the editor doesn't close itself as expected.

    By default, the delegate's event filter closes the editor whenever it loses focus, but the filter is set on the editor, not its children. When the line edit receives focus, the event filter recognizes that the new focus widget is a child of the editor and will not close it; this is important, because if the editor has child widgets, you don't want it to close just because the internal focus has changed; the side effect of this is that when clicking outside of the line edit, it is the line edit that will receive the FocusOut event, not the editor, so the event filter won't know nothing about it; note that this also happens even when using setFocusProxy(), as Qt automatically sends focus events to the proxy.

    There are at least two possible solutions for this, depending on the editor type.

    If there is going to be just one main child widget that should accept focus like in this case, the solution is to do what other complex widgets (like QComboBox and QAbstractSpinBox): set the editor as focus proxy of the widget, and post the related events to the widget in the event filter:

        def createEditor(self, parent, option, index):
            # ...
            editor.line_edit.setFocusProxy(editor)
    
        def eventFilter(self, editor, event):
            if event.type() in (
                event.FocusIn, event.FocusOut, 
                event.KeyPress, event.KeyRelease, 
                event.ShortcutOverride, event.InputMethod, 
                event.ContextMenu
                ):
                    editor.line_edit.event(event)
                    if event.type() != event.FocusOut:
                        return event.isAccepted()
            return super().eventFilter(editor, event)
    

    For more complex situations, we need to ensure that the default implementation of the delegate event filter receives a focus out event whenever the focus is completely lost by the editor or any of its children.

    In order to achieve this, a possible solution is to create a custom object that will become an event filter for all child widgets of the editor and eventually posts a FocusOut event for the editor whenever the focused widget is not itself or one of its children (including grandchildren).

    class FocusOutFilter(QObject):
        def __init__(self, widget):
            super().__init__(widget)
            self.widget = widget
            self.install(widget)
    
        def install(self, widget):
            widget.installEventFilter(self)
            for child in widget.findChildren(QWidget):
                child.installEventFilter(self)
    
        def eventFilter(self, obj, event):
            if (event.type() == event.FocusOut and
                not self.widget.isAncestorOf(QApplication.focusWidget())):
                    obj.removeEventFilter(self)
                    self.deleteLater()
                    return QApplication.sendEvent(self.widget, event)
            elif event.type() == event.ChildAdded:
                self.install(event.child())
            return super().eventFilter(obj, event)
    
    
    class CustomDelegate(QStyledItemDelegate):
        # ...
        def createEditor(self, parent, option, index):
            editor = QWidget(parent)
            editor.filter = FocusOutFilter(editor)
            # ...
    

    There is a catch. The problem is that focusWidget() will return the widget even if the event results in opening a menu. While this is fine for any context menu of the editor widgets (including the edit menu of QLineEdit), it could create some issues when the view implements the context menu event (showing a menu in contextMenuEvent(), within a ContextMenu event or with CustomContextMenu policy). There is no final solution for this, as there is no definite way to check what created the menu: a proper implementation expects that a QMenu is created with the parent that created it, but that cannot be certain (for simplicity, dynamically created menus could be created without any parent, which makes it impossible to know what created them). In these cases, the simplest solution is to implement the above conditions by first checking the return value of isPersistentEditorOpen() and ensure that widgetAt() properly returns the table's viewport().