QTableView pyqt5的奇怪行为



我刚刚进入PyQt5框架,在我的QTableView表中编辑单元格时被堆叠。

我有一个这样的模型:

class TableHMQIModel(QAbstractTableModel):
headerLabels = []
def __init__(self, data):
super(TableHMQIModel, self).__init__()
self._data = data
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 headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.headerLabels[section]
return super().headerData(section, orientation, role)
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])
def setData(self, index, value, role):
if role == Qt.EditRole and index.column() > 1 and value != "":
self._data[index.row()][index.column()] = value
return True
return False
def flags(self, index):
# if index.column() > 1:
#     return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable
return Qt.ItemIsSelectable | Qt.ItemIsEnabled

用这样的数据填充它:

data = [ ["C"+str(key), value[0], value[1], value[2], value[3], self.d_GIIP[int(key)]] for key, value in self.dict_full_HMQI.items()]
headers = ["Case", "HQMI pressure", "HQMI WUT", "HQMI total", "Cumul. condensate FC", "GIIP"]

try: 
self.tableHMQImodel = TableHMQIModel(data)
self.tableHMQImodel.headerLabels = headers
self.tableHMQI.setModel(self.tableHMQImodel)
except:
print("Something went wrong! _tableHQMI method")

问题是当表的最后一列显示的数据为空时。我在调试模式下检查了数据,没有丢失数据。实际上,所有其他表都可以使用相同的代码,但名称不同。

表:输入图片描述

我不知道到底发生了什么,但我只是包装了float()中最后一列的值,它出现了…

最新更新