如何在python中将qwidgettable中的列设置为会计格式?

How to set a column in qwidgettable as accounting format in python?

我有一个 qwidgettable,我想将特定列(我称之为 "Amount" 列)设置为会计格式,如果我键入 Eg。 1000 在我离开该单元格并转到另一个单元格后变为 1,000.00。我查看了网络,但找不到对此查询的具体响应。非常感谢您。

我确实试过这段代码,但我不知道如何完成:

self.table_data.setItemDelegateForColumn()

编辑:

这是我的代码:

import sys
from PyQt5 import QtWidgets, QtCore

class Window(QtWidgets.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50,50,500,500)
        self.setWindowTitle('PyQt Tuts')
        self.table()


    def table(self):

        self.tableWidget = QtWidgets.QTableWidget()
        self.tableWidget.setGeometry(QtCore.QRect(220, 100, 411, 392))
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setRowCount(5)
        self.tableWidget.show()

        item = QtWidgets.QTableWidgetItem()
        item.setText("Amount")
        self.tableWidget.setHorizontalHeaderItem(1, item)

        attr = ['Product1', 'Product2', 'Product3', 'Product4', 'Product5']
        i = 0
        for j in attr:
            self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(j))
            i += 1

def run():
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    sys.exit(app.exec_())

run()

这是它的样子:

我想要实现的是,当我在产品 1 的数量中输入 1000 时。当我离开该单元格后,这将自动变为 1,000.00。

不是这样的:

但是像这样:(这只是 excel 格式的快照):

QStyledItemDelegate Class

The QStyledItemDelegate class provides display and editing facilities for data items from a model.

import sys
from PyQt5 import QtWidgets, QtCore


class InitialDelegate(QtWidgets.QStyledItemDelegate):
    def __init__(self, decimals, parent=None):
        super().__init__(parent)
        self.nDecimals = decimals

    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        option.displayAlignment = QtCore.Qt.AlignCenter
        try:
            text = index.model().data(index, QtCore.Qt.DisplayRole)
            number = float(text)
            option.text = "{:,.{}f}".format(number, self.nDecimals)
        except:
            pass


class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50,50,500,500)
        self.setWindowTitle('PyQt Tuts')
        self.table()

    def table(self):
        self.tableWidget = QtWidgets.QTableWidget()
        self.tableWidget.setGeometry(QtCore.QRect(220, 100, 411, 392))
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setRowCount(5)

        delegateFloat = InitialDelegate(2, self.tableWidget)              # <---
        self.tableWidget.setItemDelegateForColumn(1, delegateFloat)       # <---
        
        self.tableWidget.show()

        item = QtWidgets.QTableWidgetItem()
        item.setText("Amount")
        self.tableWidget.setHorizontalHeaderItem(1, item)

        attr = ['Product1', 'Product2', 'Product3', 'Product4', 'Product5']
        i = 0
        for j in attr:
            self.tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(j))
            i += 1


def run():
    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    sys.exit(app.exec_())

run()