python-3.x QTableView pyqt5的异常行为

wvyml7n5  于 2022-12-30  发布在  Python
关注(0)|答案(1)|浏览(167)

我刚刚进入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")

问题是当数据显示在表的最后一列是空的。我在调试模式下检查了数据,没有丢失任何数据。从字面上看,所有其他表都可以使用相同的代码正常工作,但名称不同。
表格:enter image description here

a8jjtwal

a8jjtwal1#

我不知道到底发生了什么,但我只是把最后一列的值 Package 在float()中,它就出现了...

相关问题