Search code examples
pythonqtpysideqtableviewqabstracttablemodel

PySide QTableView setData for multiple cells


I'm using a QTableView and sub-classed a QAbstractTableModel. When editing a cell I noticed that QAbstractTableModel.setData only goes through the last selected cell. Is there a way to get setData to work with multiple (selected) cells?

As an example of usage:

  • Select four cells from the table.
  • Begin edit on one cell.
  • Input a value and hit enter to commit.
  • Have value modify all four cells (currently it just modifies the cell that is selected last)

I tried sub-classing closeEditor, then looping through selectedIndexes to call them with setData, but I don't know how to get the user's input value from closeEditor.

Here's my attempt, almost got it, just need the input value.

def closeEditor(self, editor, hint):
    is_cancelled = (hint == QtGui.QAbstractItemDelegate.RevertModelCache)

    if not is_cancelled:
        for index in self.selectedIndexes():
            if index == self.currentIndex():
                continue

            input_value = ??? # Don't know how to get this from here!

            self.model().setData(index, input_value, QtCore.Qt.EditRole)

    return QtGui.QTableWidget.closeEditor(self, editor, hint)

Solution

  • The editor in the closeEditor signal is the widget that was used for editing.

    If that is, for example a QLineEdit you can read the text of it through its text property.

    One way to retrieve the property value is through QObject's property API

    value = editor.property("text")