PDA

View Full Version : Avoiding Scientific Notation with QTableWidget while sorting numerically



bberk129
8th March 2019, 09:17
Hi,

I am constructing a table on and listing prices in a column; I want to be able to sort the column based on the number in the cell (not text).

I was using the following code to fill the cells and it was working fine for numbers up to 999,999



auto widgetItemAmount = new QTableWidgetItem;
widgetItemAmount ->setData(Qt::EditRole,query.value(8).toDouble());
ui->ordersTableWidget->setItem(i,6,widgetItemAmount);


Once the content of the cell is over 1,000,000; it is populated in scientific notation (for ex. 3,500,000 is shown as 3.5e06)
I can avoid it by using the following code:



auto widgetItemAmount = new QTableWidgetItem;
widgetItemAmount->setData(Qt::DisplayRole,QString::number(query.valu e(8).toDouble(),'f', 2));
ui->ordersTableWidget->setItem(i,6,widgetItemAmount);


However, in this case the rows sort as the contents are text (not surprisingly since they are text).

Do you have any suggestions on how I can display the value in decimal format and sort the table numerically at the same time?

Thank you for your help in advance.

anda_skoa
9th March 2019, 09:15
You can override QTableWidgetItem::operator<() in a derived class to make it use the numerical value for comparison.

Cheers,
_