헤더를 유지하면서 비어있는 2 차원 배열 QAbstractTableModel를 초기화하는 방법 : 나는 TaskModel 클래스 __init__
기능의 두 줄이난 작은 예를 들어 내 문제를 설명 할 것이다
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_())
주, 첫 번째 기부를 __task의 기본 데이터 세트로 [[ "" "," "," "]], 대신 [[]]을주는 두 번째 데이터 세트.
첫 번째는 잘 작동 : 바닥에 앉아 원치 않는 행이
것을 제외하고. 원치 않는 행을 제거하는 동안
두 번째 __init__
기능은, [[]] 기본 데이터 세트로 사용하지만, 그 결과는 재앙이다 :
내가 원치 않는 바닥 것을 제거하는 방법 행을 계속하면서 (헤더와 모든 것을 사용하여)?