Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Incorrect sizing and selection when moving columns in QTableView (QAbstractTableModel) using beginMoveColumns?
Forum Update on Monday, May 27th 2025

Incorrect sizing and selection when moving columns in QTableView (QAbstractTableModel) using beginMoveColumns?

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 347 Views
  • 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.
  • E Offline
    E Offline
    Esor
    wrote on 12 Jun 2021, 07:11 last edited by Esor
    #1

    I am trying to use beginMoveColumns to move a single column over in a QTableView, but it doesn't work properly in my example below. The cell selections get shuffled and column widths don't move correctly. Moving rows using the same logic seems to work fine. What am I doing wrong?

    Video: https://www.screencast.com/t/5UJ0iByZCEE

    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)
    
            # Shift column in each row
            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)
    
            # Shift row
            self.data.insert(new_ix, self.data.pop(ix))
    
            self.endMoveRows()
    
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
    
        data = []
        for i in range(12):
            row = []
            data.append(row)
            for j in range(6):
                row.append(f"R{i}_C{j}")
    
        model = MyTableModel(data)
        view = QtWidgets.QTableView()
        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.resize(800, 800)
        container.show()
    
    
        sys.exit(app.exec_())
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Jun 2021, 18:48 last edited by
      #2

      Hi,

      Can you check with 5.15 ?

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

      E 1 Reply Last reply 12 Jun 2021, 19:29
      0
      • S SGaist
        12 Jun 2021, 18:48

        Hi,

        Can you check with 5.15 ?

        E Offline
        E Offline
        Esor
        wrote on 12 Jun 2021, 19:29 last edited by Esor 6 Dec 2021, 20:53
        #3

        @SGaist said in Incorrect sizing and selection when moving columns in QTableView (QAbstractTableModel) using beginMoveColumns?:

        Hi,

        Can you check with 5.15 ?

        It happens with all versions of 5.15, 5.14 and 5.13.

        I was mistaken in my initial post where I said it was working on some versions, I edited that part out.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Esor
          wrote on 13 Jun 2021, 04:08 last edited by Esor
          #4

          I did some more experimenting and it does seem to change between versions but is broken in every version... all the versions listed below which I tested have cell selections not moving properly when moving columns, and every version EXCEPT 5.14.1 has column widths not being maintained properly when moving. In every version, moving rows with the same technique works fine.

          5.13.2: https://www.screencast.com/t/mPXbeA8Rjp
          5.14.1: https://www.screencast.com/t/d8lRFMuM04HJ
          5.14.2: https://www.screencast.com/t/n1HXqvzr
          5.15.2: https://www.screencast.com/t/kKAL4xiJJXuk
          5.15.4: https://www.screencast.com/t/smLdJb8Tb

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 13 Jun 2021, 19:19 last edited by
            #5

            Thanks for the thorough analysis.

            You should head to the bug report system and open issue there providing your minimal example and video demonstration. Note that there might already be a ticket about it. In which case you can add your findings to it.

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

            1 Reply Last reply
            0

            1/5

            12 Jun 2021, 07:11

            • Login

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