簡體   English   中英

QTableView 的奇怪行為 pyqt5

[英]Strange behavior of QTableView pyqt5

我剛剛進入 PyQt5 框架並在我的 QTableView 表中編輯單元格時被堆疊。

我有一個這樣的 model:

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() 中,它出現了……

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM