python - Python PyQt5 - 如何重命名表头?
问题描述
我有一个向表添加新列的函数,但是我不知道如何重命名列标题。我想要的是用户双击列标题,以便您可以编辑列标题。或者我现在拥有的:我有一个按钮,可以添加一个新列和行编辑,您可以在其中写入列名。
我不知道如何重命名列标题名称
这是我处理向表中添加新列的代码。
def addNewColumn(self):
self.tableColumn = self.table_Database.columnCount()
self.table_Database.setColumnCount(self.tableColumn + 1)
self.table_Database.setItem(0,self.tableColumn,QtWidgets.QTableWidgetItem())
if self.table_Database.rowCount() == 0:
self.table_Database.setRowCount(1)
self.table_Database.setItem(1,0,QtWidgets.QTableWidgetItem())
解决方案
如果您使用模型.. 请参阅 headerData... 它定义了水平标题... 列。
class TableModel(QtCore.QAbstractTableModel):
def __init__(self, data):
super(TableModel, self).__init__()
self._data = data
def headerData(self, p_int, Qt_Orientation, role=None):
if role == Qt.DisplayRole and Qt_Orientation==Qt.Horizontal:
header = ['Symbol', 'Time', 'Price', 'Current Price','Orig Qty', 'Executed Qty', 'Type']
return header[p_int]
else:
return QtCore.QAbstractTableModel.headerData(self, p_int, Qt_Orientation, role)
def data(self, index, role):
if role == Qt.DisplayRole:
# See below for the nested-list data structure.
# .row() indexes into the outer list,
# .column() indexes into the sub-list
return self._data[index.row()][index.column()]
def rowCount(self, index):
# The length of the outer list.
return len(self._data)
def columnCount(self, index):
# The following takes the first sub-list, and returns
# the length (only works if all rows are an equal length)
return len(self._data[0])
推荐阅读
- java - Selenium 如何从弹出通知中获取文本
- stored-procedures - 推断存储在 cassandra 列中每行的 JSON 文件的字节大小
- java - 在 java 中使用 hasrsine 公式或一些高级 postgis 查询?
- drools - Drools 7.39.0 是否支持 jBoss EAP
- python - 尝试使用 any() 更改 url 中的单词
- python - 如何使用python将列中的列表拆分为数据框中的两列?
- java - 如何将大型 POJO 响应从 Spring Boot 发送到客户端
- python - 如何将部分列列表转换为日期时间
- c - 在代码文件上定义函数但不在标题上声明它是一种好习惯吗?
- python - 从另一个对象 B 本身访问对象 A 的属性 A 的属性