How do I move a column in a QTableView (QAbstractTableModel) using beginMoveColumns?

I am trying to move columns in my QTableView in the example below, but am struggling to understand how to correctly call beginMoveColumns. My example has buttons to apply row and column movements, but when I move columns the column widths and cell selections don't seem to get properly moved along with the data. What am I doing wrong?

from PyQt5 import QtWidgets, QtCore, QtGui
import sys

from PyQt5.QtCore import QModelIndex, Qt


class MyTableModel(QtCore.QAbstractTableModel):
    def __init__(self, data=[[]], parent=None):
        super().__init__(parent)
        self.data = data

    def headerData(self, section: int, orientation: Qt.Orientation, role: int):
        if role == QtCore.Qt.DisplayRole:
            return "Header"

    def columnCount(self, parent=None):
        return len(self.data[0])

    def rowCount(self, parent=None):
        return len(self.data)

    def data(self, index: QModelIndex, role: int):
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            col = index.column()
            return str(self.data[row][col])

    def move_column(self, ix, new_ix):
        parent = QtCore.QModelIndex()
        if new_ix > ix:
            target = new_ix + 1
        else:
            target = new_ix
        self.beginMoveColumns(parent, ix, ix, parent, target)

        for row in self.data:
            row.insert(new_ix, row.pop(ix))

        self.endMoveColumns()

    def move_row(self, ix, new_ix):
        parent = QtCore.QModelIndex()
        if new_ix > ix:
            target = new_ix + 1
        else:
            target = new_ix
        self.beginMoveRows(parent, ix, ix, parent, target)

        self.data.insert(new_ix, self.data.pop(ix))

        self.endMoveRows()


class MyTableView(QtWidgets.QTableView):
    def __init__(self, parent=None):
        super().__init__(parent)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)

    data = [[11, 12, 13, 14, 15],
            [21, 22, 23, 24, 25],
            [31, 32, 33, 34, 35],
            [41, 42, 43, 44, 45],
            [51, 52, 53, 54, 55],
            [61, 62, 63, 64, 65]]

    model = MyTableModel(data)
    view = MyTableView()
    view.setModel(model)

    view.setColumnWidth(0, 50)
    view.setColumnWidth(1, 100)

    view.setRowHeight(0, 50)
    view.setRowHeight(1, 100)

    container = QtWidgets.QWidget()
    layout = QtWidgets.QVBoxLayout()
    container.setLayout(layout)
    layout.addWidget(view)

    button = QtWidgets.QPushButton("Move 1st column right by 1")
    button.clicked.connect(lambda: model.move_column(0, 1))
    layout.addWidget(button)

    button = QtWidgets.QPushButton("Move 1st column right by 2")
    button.clicked.connect(lambda: model.move_column(0, 2))
    layout.addWidget(button)

    button = QtWidgets.QPushButton("Move 1st row down by 1")
    button.clicked.connect(lambda: model.move_row(0, 1))
    layout.addWidget(button)

    button = QtWidgets.QPushButton("Move 1st row down by 2")
    button.clicked.connect(lambda: model.move_row(0, 2))
    layout.addWidget(button)

    container.show()
    sys.exit(app.exec_())


Read more here: https://stackoverflow.com/questions/67927339/how-do-i-move-a-column-in-a-qtableview-qabstracttablemodel-using-beginmovecolu

Content Attribution

This content was originally published by Esostack at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: