Results 1 to 2 of 2

Thread: Avoiding Scientific Notation with QTableWidget while sorting numerically

  1. #1
    Join Date
    Mar 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Avoiding Scientific Notation with QTableWidget while sorting numerically

    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

    Qt Code:
    1. auto widgetItemAmount = new QTableWidgetItem;
    2. widgetItemAmount ->setData(Qt::EditRole,query.value(8).toDouble());
    3. ui->ordersTableWidget->setItem(i,6,widgetItemAmount);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. auto widgetItemAmount = new QTableWidgetItem;
    2. widgetItemAmount->setData(Qt::DisplayRole,QString::number(query.value(8).toDouble(),'f', 2));
    3. ui->ordersTableWidget->setItem(i,6,widgetItemAmount);
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Avoiding Scientific Notation with QTableWidget while sorting numerically

    You can override QTableWidgetItem::operator<() in a derived class to make it use the numerical value for comparison.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 11th August 2015, 00:26
  2. Replies: 3
    Last Post: 17th April 2010, 22:35
  3. Sorting a qtablewidget......
    By reshma in forum Qt Programming
    Replies: 8
    Last Post: 23rd March 2009, 15:34
  4. qdoublespinbox with scientific notation
    By pospiech in forum Qt Programming
    Replies: 13
    Last Post: 3rd January 2009, 15:50
  5. QTableWidget Sorting
    By mclark in forum Newbie
    Replies: 4
    Last Post: 29th September 2006, 23:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.