我已經(jīng)定義我的自定義QAbstrtactTableModelPython和實(shí)施columnCount(),rowCount(),data()和headerData()和也加入到一個(gè)函數(shù)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)數(shù)據(jù)是一個(gè) python 字典,通過(guò)調(diào)用隨機(jī)函數(shù)進(jìn)行修改,該函數(shù)每 1 秒用 1 個(gè)條目填充字典,如下所示:"0,0":[隨機(jī)值]"0,1":[隨機(jī)值]"0,2":[隨機(jī)值]"1,0":[隨機(jī)值]"1,1":[隨機(jī)值]"1,2":[隨機(jī)值]rowCount()正確反映數(shù)據(jù)中的行數(shù)values,但tableView始終只顯示 1 行,并且data()僅請(qǐng)求index.row() == 0
添加回答
舉報(bào)
0/150
提交
取消