모델의 some signals은 항목의 하위 항목이 삽입되거나 제거 될 때마다 실행되므로 항목을 자동으로 업데이트하는 데 사용할 수 있습니다.
model.rowsInserted.connect(slot, type=QtCore.Qt.QueuedConnection)
model.rowsRemoved.connect(slot, type=QtCore.Qt.QueuedConnection)
하지만 그 이외는, 구현이 매우 간단합니다 :
몇 가지 실험 후, 나는 모델이 완벽하게 업데이트 할 수있는 기회를 가질 수 있도록 신호가하는
queued connection로 사용할 필요가 있음을 발견했다. 업데이트가 동적으로 수행 될 수 있으므로 항목에 추가 정보를 저장할 필요가 없습니다. 여기
기본적인 데모 스크립트입니다 : 바로 드래그 앤 항목을 드롭으로, 현재의 시도와
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QTreeView):
def __init__(self):
super(Window, self).__init__()
self.setDragDropMode(QtWidgets.QAbstractItemView.InternalMove)
self.setDragDropOverwriteMode(False)
self.header().hide()
model = QtGui.QStandardItemModel(self)
model.rowsInserted.connect(
self.sumItems, type=QtCore.Qt.QueuedConnection)
model.rowsRemoved.connect(
self.sumItems, type=QtCore.Qt.QueuedConnection)
self.setModel(model)
parent = model.invisibleRootItem()
for index in range(3):
item = QtGui.QStandardItem('0')
parent.appendRow(item)
for row in range(1, 5):
child = QtGui.QStandardItem(str(row))
item.appendRow(child)
self.expandAll()
def sumItems(self, index, first, last):
if index.isValid():
total = 0
parent = self.model().itemFromIndex(index)
for row in range(parent.rowCount()):
child = parent.child(row)
if child is not None:
total += int(child.text())
parent.setText(str(total))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setGeometry(700, 100, 250, 300)
window.show()
sys.exit(app.exec_())
, 저장된 모든 자식의 인덱스 (모든 자녀 등) 즉시있을 것입니다 무효화 됨. 따라서 이러한 유형의 접근 방식은 구현 방법에 관계없이 작동하지 않습니다. 이 작업을 통해 해결하려는 실제 문제는 무엇입니까? – ekhumoro
항목 행을 트리에서 제거하고이를 트리 내의 다른 위치에 놓을 때 열의 합계를 업데이트하려고합니다. 각 부모 노드에는 열에있는 자식 항목의 합계를 유지하는 합계 항목이 있습니다. 항목을 안팎으로 이동할 때마다 해당 합계 항목을 업데이트하려고합니다. 나는 이것이 어떤 의미가되기를 바란다! – Zexelon