0
我将演示一个小例子我的问题:如何用一个空二维数组初始化QAbstractTableModel同时保持头
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_())
请注意,我有__init__
功能的两根线TaskModel类,第一个捐赠[[“”,“”,“”,“”]]作为__task的默认数据集,第二个给出[[]]代替。
第一个正常工作:
,除了有一排无用坐在底部。
第二__init__
功能,同时试图删除不需要的行,使用[[]]作为默认的数据集,但结果是灾难性的:
我怎样可以删除不需要的底部行,而仍然使其工作(与标题和一切)?