简体   繁体   English

如何保存编辑的 PyQt5 TableWidget 单元格?

[英]How to save edited PyQt5 TableWidget cell?

I have an Application which has a QTableWidget.我有一个具有 QTableWidget 的应用程序。 When the user clicks a cell he can edit it, and can write anything.当用户点击一个单元格时,他可以编辑它,并且可以写任何东西。 When he is finished he presses the 'enter' key to finish editing the cell.完成后,他按下“输入”键以完成对单元格的编辑。

I want to get the new cell value right after the enter key and save it to a file with the keypress.我想在输入键后立即获取新的单元格值,并使用按键将其保存到文件中。

    #keyPressEvent
    self.tableWidget.keyPressEvent = self.KeyPressed

def KeyPressed(self,event):
    if event.key() == Qt.Key_Return:
        row = self.tableWidget.currentRow()
        col = self.tableWidget.currentColumn()
        print(self.tableWidget.item(row, col).text())

This is the keypress code, but it gets the old value when the key pressed.这是按键代码,但它在按键时获取旧值。

Any solution for this, or any other way to save the edited cell to a file?任何解决方案,或任何其他将编辑的单元格保存到文件的方法?

You have to call this in your main class:你必须在你的主 class 中调用它:

self.tableWidget.itemChanged.connect(self.save_changes)

@pyqtSlot()
def save_changes(self):
    for currentQTableWidgetItem in self.tableWidget.selectedItems():
        projectName = self.tableWidget.item(currentQTableWidgetItem.row(),  0).text()
        #save changes to a .csv file with pandas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM