[PyQt] question re: QVariant formatting in QTableWidgetItem

Matt Newell newellm at blur.com
Sun May 11 02:46:06 BST 2008

On Saturday 10 May 2008 11:49:38 Brian Gyss wrote:
> All:
> I'm having some issues changing the formatting of a number in a
> QTableWidgetItem.  It seems that whenever I feed any sort of number
> with non-zero digits in front of a decimal point into the QVariant
> type, the QTableWidgetItem will always display the formatting to 6
> digits in a specific format.
> For example, a number like this:
> 10454.450432
> will always be depicted like this:
> 10,454.4
> when the specific item is inserted in a table.  Also, I need to use
> QTableWIdgetItem.setData() in order to insert the numeric data or else
> the numbers in the table are not sortable.
> Do I have to subclass either QVariant or QTableWidgetItem in order to
> change the formatting of how numbers are depicted inside of
> QTableWidget cells, or is there a more succinct way of doing this?
> Thanks!
> - Brian

I think you have 2 options.

1) Subclass QTableWidgetItem.  override the data function, return a formatted 
string for DisplayRole, and float/double for EditRole.  You'll also have to 
override the operator< to get proper sorting, because I believe the default 
implemenation uses the DisplayRole for sorting.

2) Subclass QItemDelegate and have it do the formatting. 

#1 is probably easier.


More information about the PyQt mailing list