0

小さな例で私の問題を示します。

import sys
from PySide import QtCore, QtGui

class TaskModel(QtCore.QAbstractTableModel):
    def __init__(self, tasks=[[" ", " ", " ", " "]]):
    # def __init__(self, tasks=[[]]):
        super().__init__()
        self.__tasks = tasks
        self.__headers = ["Folder", "Command", "Patterns", "Active", "Recursive"]

    def rowCount(self, *args, **kwargs):
        return len(self.__tasks)

    def columnCount(self, *args, **kwargs):
        coln = len(self.__tasks[0])
        return coln

    def headerData(self, section, orientation, role):
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return self.__headers[section]
            else:
                # set row names: color 0, color 1, ...
                return "%s" % str(section+1)

    def data(self, index, role):
        row = index.row()
        col = index.column()
        value = self.__tasks[row][col]

        # text content
        if role == QtCore.Qt.DisplayRole:
            return value


    def insertRows(self, position, rows, parent=QtCore.QModelIndex()):
        self.beginInsertRows(parent, position, position + rows - 1)
        row = ["a", "b", "c", "d"]
        for i in range(rows):
            self.__tasks.insert(position, row)
        self.endInsertRows()
        return True

class Mc(QtGui.QWidget):
    def __init__(self):
        super().__init__()
        self.tab = QtGui.QTableView()
        self.model = TaskModel()
        self.tab.setModel(self.model)
        self.addbtn = QtGui.QPushButton("Add")
        self.addbtn.clicked.connect(self.insert)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.tab)
        layout.addWidget(self.addbtn)
        self.setLayout(layout)

    def insert(self):
        self.model.insertRow(0)

app = QtGui.QApplication(sys.argv)
mc = Mc()
mc.show()
sys.exit(app.exec_())

TaskModel クラスには 2 行の__init__関数があることに注意してください。最初の行は [[" ", " ", " ", " "]] を __task のデフォルト データ セットとして指定し、2 行目は代わりに [[]] を指定します。

最初のものはうまくいきます:

ここに画像の説明を入力

一番下に不要な行があることを除いて。

2 番目の__init__関数は、不要な行を削除しようとして [[]] をデフォルトのデータセットとして使用しましたが、結果は悲惨なものになりました。

ここに画像の説明を入力

不要な一番下の行を削除しながら、ヘッダーとすべてを機能させるにはどうすればよいですか?

4

1 に答える 1