【发布时间】:2021-08-27 19:50:37
【问题描述】:
我试图在下面的示例中移动我的 QTableView 中的行,但我很难理解如何正确调用 beginMoveRows。我的示例有 3 个按钮来执行各种行移动,而第三个按钮(将行向下移动 1)会导致我的程序崩溃。
我认为它崩溃是因为这个声明in the docs...
请注意,如果 sourceParent 和 destinationParent 相同,则必须确保 destinationChild 不在 sourceFirst 和 sourceLast + 1 的范围内。还必须确保不要尝试将行移动到其自己的子节点之一或祖先。如果任一条件为真,则此方法返回 false,在这种情况下,您应该中止移动操作。
但是这个限制不是意味着我不能将一行向下移动 1 吗?在我的情况下 sourceLast==sourceFirst 因为我一次只移动一行所以这个语句基本上说我的目标索引不能等于我的源索引 + 1,这是将行向下移动 1 的定义。我是误解了这些论点的含义?在此示例中,如何将行向下移动 1 个位置?
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:
if orientation == Qt.Horizontal:
return "Column " + str(section)
else:
return "Row " + str(section)
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])
class MyTableView(QtWidgets.QTableView):
def __init__(self, parent=None):
super().__init__(parent)
def move_row(self, ix, new_ix):
full_index = self.model().index(0, self.model().rowCount())
self.model().beginMoveRows(full_index, ix, ix, full_index, new_ix)
data = self.model().data
data.insert(new_ix, data.pop(ix))
self.model().endMoveRows()
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)
container = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout()
container.setLayout(layout)
layout.addWidget(view)
button = QtWidgets.QPushButton("Move 3rd row down by 2")
button.clicked.connect(lambda: view.move_row(2, 2 + 2))
layout.addWidget(button)
button = QtWidgets.QPushButton("Move 3rd row up by 1")
button.clicked.connect(lambda: view.move_row(2, 2 - 1))
layout.addWidget(button)
button = QtWidgets.QPushButton("Move 3rd row down by 1 (This fails)")
button.clicked.connect(lambda: view.move_row(2, 2 + 1))
layout.addWidget(button)
container.show()
sys.exit(app.exec_())
【问题讨论】:
标签: python pyqt qtableview qabstracttablemodel