gpt4 book ai didi

python - QTableView 未在 dataChanged 上更新

转载 作者:太空宇宙 更新时间:2023-11-04 05:31:28 27 4
gpt4 key购买 nike

当模型数据更改时,我无法更新 QTableView 的布局。我尝试使用 dataChanged.emit(index,index)、layoutChanged.emit() 以及最后的手段 reset()。没有一个奏效。我的代码:

class SettingsDialog(QDialog, settings_design.Ui_settingsDialog):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.weekdayTable = QTableView(self.weekdayPage)
weekdayModel = self.loadMeetingData(self.meetingsData['weekdayList'])
#weekdayModel.dataChanged.connect(self.updateTable)
self.weekdayTable.setModel(weekdayModel)

def updateTable(self):
self.weekdayTable.repaint()

class MeetingsModel(QStandardItemModel):
def __init__(self, data, columns):
QStandardItemModel.__init__(self, data, columns)
with open('meetings.config.json') as f:
self.meetingsData = json.load(f)

def setData(self, index, value, other):

self.meetingsData['weekdayList'][int(index.row())][index.column()] = value
with open('meetings.config.json', 'w+') as f:
f.write(json.dumps(self.meetingsData))

self.dataChanged.emit(index, index)
# self.layoutChanged.emit(index, index)
return True

我做错了什么?

最佳答案

我最终通过其他方法解决了这个问题。

旧方法(优雅的方法):

我扩展了 QStandardModelItemsetData 方法,将修改后的数据保存到文件中。

新方法:

dataChanged signalQStandarItemModel 发出时,连接的 slot (writeChangedData) 将更改的数据写入文件。换句话说,我将功能从 setData 移到了插槽中。

代码:

class SettingsDialog(QDialog, settings_design.Ui_settingsDialog):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
self.weekdayTable = QTableView(self.weekdayPage)
self.weekdayModel = self.loadMeetingData(self.meetingsData['weekdayList'])
self.weekdayModel.dataChanged.connect(self.writeModifiedData)
self.weekdayTable.setModel(self.weekdayModel)

def writeModifiedData(self, topLeft, bottomRight):
self.weekdayModel['weekdayList'][int(topLeft.row())][topLeft.column()] = value
with open('meetings.config.json', 'w+') as f:
f.write(json.dumps(self.weekdayModel))
f.close()

关于python - QTableView 未在 dataChanged 上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37034190/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com