亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

QAbstractTabelModel.data() 總是得到 index.row() == 0

QAbstractTabelModel.data() 總是得到 index.row() == 0

素胚勾勒不出你 2021-06-29 17:53:21
我已經定義我的自定義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
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號