我已經定義我的自定義QAbstrtactTableModelPython和實施columnCount(),rowCount(),data()和headerData()和也加入到一個函數add()新條目:import sysfrom PySide2.QtUiTools import QUiLoaderfrom PySide2.QtWidgets import QApplication, QMainWindowfrom PySide2.QtCore import QFile, QProcess, Signal,\ Slot, QObject, QThread, Qt, QAbstractTableModel, QModelIndex,\ QTimerimport randomclass TableModel(QAbstractTableModel): def __init__(self, values): QAbstractTableModel.__init__(self) self.values = values def columnCount(self, parent=QModelIndex()): return 2 def rowCount(self, parent=QModelIndex()): return len(self.values) def data(self, index, role=Qt.DisplayRole): print("called") if 0 <= index.row() < self.rowCount() and 0 <= index.column() < self.columnCount(): if role == Qt.DisplayRole: print(index.row()) if index.column() == 0: return list(self.values.keys())[index.row()] else: return self.values[list(self.values.keys())[index.row()]] def add(self, data): self.values[data[0]] = data[1] def headerData(self, section, orientation, role=Qt.DisplayRole): if role == Qt.DisplayRole: if orientation == Qt.Horizontal: if section == 0: return "Parking,Meter" elif section == 1: return "Revenue"class Monitor(QObject): data_batch_ready = Signal(list) def __init__(self, parent=None): super(Monitor, self).__init__(parent) @Slot(list) def fill_batch(self): my_data = [] for parking in range(4): for index in range(10): my_data.append([str(parking)+","+str(index), random.randint(1,101)]) self.data_batch_ready.emit(my_data)數據是一個 python 字典,通過調用隨機函數進行修改,該函數每 1 秒用 1 個條目填充字典,如下所示:"0,0":[隨機值]"0,1":[隨機值]"0,2":[隨機值]"1,0":[隨機值]"1,1":[隨機值]"1,2":[隨機值]rowCount()正確反映數據中的行數values,但tableView始終只顯示 1 行,并且data()僅請求index.row() == 0
添加回答
舉報
0/150
提交
取消