如何在PyQt5的QTableWidget的handleItemChanged中获取旧值。

huangapple go评论64阅读模式
英文:

How to get an old_value from handleItemChanged on QTableWidget in PyQt5

问题

handleItemChanged函数中,您可以获取编辑的项目的当前值,但要获取先前的值,您可以使用一个字典来存储项目的先前值。以下是您可以将其添加到代码中的修改部分:

class MyWindow(QMainWindow):
    def __init__(self):
        # ... 其他代码不变 ...

        # Create a dictionary to store previous values
        self.previous_values = {}

        # Connect handleItemChanged to QTableWidget
        self.table_widget.itemChanged.connect(self.handleItemChanged)

    def handleItemChanged(self, item):
        # Get row and column of edited item
        row = item.row()
        column = item.column()

        # Get current value
        current_value = item.text()

        # Get previous value from the dictionary
        previous_value = self.previous_values.get((row, column), None)

        print(f'Edited row: {row}, column: {column}')
        print(f'Previous value: {previous_value}')
        print(f'Current value: {current_value}')

        if current_value != previous_value:
            print("Do something important here!")

        # Update the previous value in the dictionary
        self.previous_values[(row, column)] = current_value

通过这种方式,您将能够在handleItemChanged函数中跟踪先前值和当前值,以进行比较。

英文:

I have a question related to QTableWidget and handleItemChanged.
Is there a way to get the before change and after the change values for comparison?

import sys
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # Create QTableWidget and add to QMainWindow
        self.table_widget = QTableWidget(self)
        self.setCentralWidget(self.table_widget)

        # Set column count and headers
        self.table_widget.setColumnCount(3)
        self.table_widget.setHorizontalHeaderLabels(['Name', 'Age', 'Gender'])

        # Set row count and items
        self.table_widget.setRowCount(3)
        self.table_widget.setItem(0, 0, QTableWidgetItem('John'))
        self.table_widget.setItem(0, 1, QTableWidgetItem('30'))
        self.table_widget.setItem(0, 2, QTableWidgetItem('Male'))
        self.table_widget.setItem(1, 0, QTableWidgetItem('Mary'))
        self.table_widget.setItem(1, 1, QTableWidgetItem('25'))
        self.table_widget.setItem(1, 2, QTableWidgetItem('Female'))
        self.table_widget.setItem(2, 0, QTableWidgetItem('Bob'))
        self.table_widget.setItem(2, 1, QTableWidgetItem('40'))
        self.table_widget.setItem(2, 2, QTableWidgetItem('Male'))

        # Connect handleItemChanged to QTableWidget
        self.table_widget.itemChanged.connect(self.handleItemChanged)

    def handleItemChanged(self, item):
        # Get row and column of edited item
        row = item.row()
        column = item.column()

        # Get value before and after change
        current_value = item.text()
        previous_value = None # <--- is there a way to get a previous value

        print(f'Edited row: {row}, column: {column}')
        print(f'Previous value: {previous_value}')
        print(f'Current value: {current_value}')
        
        if current_value != previous_value:
            print("Do something important here!")


def main():
    app = QApplication(sys.argv)
    table = MyWindow()
    table.show()
    app.exec_()

main()

If it's not possible would you advise a solution?

Thank You very much for all your help.

答案1

得分: 1

我不知道是否有其他更好的方法来完成这个任务,但我认为你可以在更改值之前保存该值。因为要更改它,你需要先选择该项,所以你可以处理选择操作,并在那一刻存储数据,以便在更改项目时使用。

如果项目没有更改,什么都不会发生,因为当你选择另一个项目进行更改时,它将覆盖该值。

import sys
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        # 创建 QTableWidget 并添加到 QMainWindow
        self.table_widget = QTableWidget(self)
        self.setCentralWidget(self.table_widget)

        # 设置列数和表头
        self.table_widget.setColumnCount(3)
        self.table_widget.setHorizontalHeaderLabels(['Name', 'Age', 'Gender'])

        # 设置行数和项目
        self.table_widget.setRowCount(3)
        self.table_widget.setItem(0, 0, QTableWidgetItem('John'))
        self.table_widget.setItem(0, 1, QTableWidgetItem('30'))
        self.table_widget.setItem(0, 2, QTableWidgetItem('Male'))
        self.table_widget.setItem(1, 0, QTableWidgetItem('Mary'))
        self.table_widget.setItem(1, 1, QTableWidgetItem('25'))
        self.table_widget.setItem(1, 2, QTableWidgetItem('Female'))
        self.table_widget.setItem(2, 0, QTableWidgetItem('Bob'))
        self.table_widget.setItem(2, 1, QTableWidgetItem('40'))
        self.table_widget.setItem(2, 2, QTableWidgetItem('Male'))
        self.table_widget.itemPressed.connect(self.selectedItemValue)
        # 连接 handleItemChanged 到 QTableWidget
        self.table_widget.itemChanged.connect(self.handleItemChanged)

    def selectedItemValue(self, item):
        self.previous_value = item.text()

    def handleItemChanged(self, item):
        # 获取编辑项目的行和列
        row = item.row()
        column = item.column()

        # 获取更改前后的值
        current_value = item.text()
        previous_value = self.previous_value

        print(f'编辑的行: {row}, 列: {column}')
        print(f'之前的值: {previous_value}')
        print(f'当前的值: {current_value}')
        
        if current_value != previous_value:
            print("在这里执行重要操作!")

def main():
    app = QApplication(sys.argv)
    table = MyWindow()
    table.show()
    app.exec_()

main()

这是你提供的代码的翻译部分。

英文:

I don't know if there is any other better way to do this than the following answer, but what I thought is that you could save the value before changing, and because for changing it you need to select the item first, you can handle that and store the data in that moment to use it when the item is changed.

If the item is not changed nothing happen because when you select another one to change it, it will override the value.

import sys
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
# Create QTableWidget and add to QMainWindow
self.table_widget = QTableWidget(self)
self.setCentralWidget(self.table_widget)
# Set column count and headers
self.table_widget.setColumnCount(3)
self.table_widget.setHorizontalHeaderLabels(['Name', 'Age', 'Gender'])
# Set row count and items
self.table_widget.setRowCount(3)
self.table_widget.setItem(0, 0, QTableWidgetItem('John'))
self.table_widget.setItem(0, 1, QTableWidgetItem('30'))
self.table_widget.setItem(0, 2, QTableWidgetItem('Male'))
self.table_widget.setItem(1, 0, QTableWidgetItem('Mary'))
self.table_widget.setItem(1, 1, QTableWidgetItem('25'))
self.table_widget.setItem(1, 2, QTableWidgetItem('Female'))
self.table_widget.setItem(2, 0, QTableWidgetItem('Bob'))
self.table_widget.setItem(2, 1, QTableWidgetItem('40'))
self.table_widget.setItem(2, 2, QTableWidgetItem('Male'))
self.table_widget.itemPressed.connect(self.selectedItemValue)
# Connect handleItemChanged to QTableWidget
self.table_widget.itemChanged.connect(self.handleItemChanged)
def selectedItemValue(self, item):
self.previous_value = item.text()
def handleItemChanged(self, item):
# Get row and column of edited item
print(type(item))
row = item.row()
column = item.column()
# Get value before and after change
current_value = item.text()
previous_value = self.previous_value # <--- is there a way to get a previous value
print(f'Edited row: {row}, column: {column}')
print(f'Previous value: {previous_value}')
print(f'Current value: {current_value}')
if current_value != previous_value:
print("Do something important here!")
def main():
app = QApplication(sys.argv)
table = MyWindow()
table.show()
app.exec_()
main()

huangapple
  • 本文由 发表于 2023年3月9日 18:38:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定