Tableview使用QSqlRelationalTableModel。如何从垂直标题中的列插入数据?
答案 0 :(得分:0)
最简单的方法是继承QSqlRelationalTableModel
并重写headerData
方法(负责显示标题数据:
class MyModel(QtSql.QSqlRelationalTableModel):
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if orientation == QtCore.Qt.Vertical:
index = self.index(section, 0) # assuming ID is the first column
return self.data(index, role)
else:
# return original headers for horizontal orientation
return super(MyModel, self).headerData(section, orientation, role)
然后使用此代替QSqlRelationalTableModel
。如果需要,您可以在ID
QTableView
中隐藏setColumnHidden
列(因为它会重复):
myTableView.setColumnHidden(0, True) # again assuming ID is the first column