1

コードはほぼ完成です。契約は次のとおりです。

python と PySide です。QAbstractTableModel と QTableView があります。

行を正しく削除できません。問題は行のインデックスのどこかにあると思います。そのうちの1つを削除します...

ここに私が使用するボタンデリゲートがあります:

class ButtonDelegate(QItemDelegate):

    def __init__(self, parent):
        QItemDelegate.__init__(self, parent)

    def paint(self, painter, option, index):

        widget = QWidget()
        layout = QHBoxLayout()
        widget.setLayout(layout)
        btn = QPushButton("X")
        btn.clicked.connect(partial(self.parent().cellButtonClicked, index))
        layout.addWidget(btn)
        layout.setContentsMargins(2,2,2,2)

        if not self.parent().indexWidget(index):
            self.parent().setIndexWidget(index, widget)

これが cellButtonClicked メソッドです。テーブル ビューの下にあります。

class Table(QTableView):

def __init__(self, *args, **kwargs):
    QTableView.__init__(self, *args, **kwargs)

    self.setItemDelegateForColumn(6, ButtonDelegate(self))
    self.setItemDelegateForColumn(0, EmptyDelegate(self))

    self.setSortingEnabled(True)

def cellButtonClicked(self, index,  *args):

    model = self.model()
    model.removeRow(index.row())

モデルのremoveRowメソッドは次のとおりです。

def removeRow(self, row, parent = QtCore.QModelIndex()):

    self.beginRemoveRows(parent, row, row)

    array = []
    for i in range(7):
        if i == 0:
            array.append(self.index(row, i).data())
        else:
            array.append(str(self.index(row, i).data()))

    self.cycles.remove(array)

    self.endRemoveRows()

    # update custom node in maya. 
    self.getData()

主に問題は、行を削除してもモデルのインデックスが更新されないことだと思います。そのため、削除ボタンをもう一度クリックすると、モデルのrowCountと一致しなくなったインデックスでremoveRow()が開始されるため、モデルデータから削除する配列を構築できません。

意味がありましたか?さらにコードが必要な場合は、必要なものを教えてください。

4

1 に答える 1