简体   繁体   中英

PyQt - Formatting numbers in QAbstractTableModel

I have a question regarding how to format numbers in a DataFrame inside PyQt. I want to see numbers be displayed as 1,004,123 instead of 1004123.

I have created a QAbstractTableModel with headerData , rowCount , columnCount , and data functions, however I am struggling to find the procedure to format the numbers.

I believe I need to modify the QVariant input but this is where I have the problems converting to a formatted string.

def data(self, index, role = Qt.DisplayRole):
    if role == Qt.DisplayRole:
        value = self.datain.values[index.row()][index.columns()]
        return QVariant(value)

Any help would be greatly appreciated!

Thanks!

just to be sure 1,004,123 is no longer number but its string. You can achieve it by following:

number = 1004123
print(number)
string = "{:,}".format(number)
print(string)

Output:

1004123

1,004,123

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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