I am just getting into PyQt5 framework and got stacked while editing cell in my QTableView table.
I have a model like that:
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
And filling it with data like that:
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")
The problem is when the data is shown in table last column is empty. I checked in debugging mode the data and no data is being missed. Literally all the other tables work fine with the same code, but different names of course.
Table: enter image description here
I don't know what exactly was happening, but i just wrapped the value for the last column in float() and it showed up...