Results 1 to 6 of 6

Thread: QAbstractItemModel, QTableWidget. Decimal point.

  1. #1
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QAbstractItemModel, QTableWidget. Decimal point.

    In float and double values displayed in QTableWidget connected to QAbstractItemModel I want to display comma instead of international decimal point.
    How can I do it?

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

    Default Re: QAbstractItemModel, QTableWidget. Decimal point.

    So you are running the program in a locale setting that has point as the decimal separator?
    Do you want that only in a specific table or througout the whole program?

    Cheers,
    _

  3. #3
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractItemModel, QTableWidget. Decimal point.

    I would be glad for the both answers but especially for the specific table case.
    I tried: http://www.qtcentre.org/threads/5312...-QSystemLocale (code from the last post, but with comma).

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractItemModel, QTableWidget. Decimal point.

    Floating point numbers are usually formatted according to the locale of the user, e.g. using a point in countries where this is common and a comma in those which use that separator.

    An application can of course override this, e.g. if it provides its own locale settings.

    If you only need it in a particular case, you can use QLocale::toString() on a specifically created QLocale object.
    In your case using an item delegate for that cell, column/row or whole table, or by overriding the model's data to return a different string for the respective cells' DisplayRole.

    Cheers,
    _

  5. #5
    Join Date
    Oct 2012
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractItemModel, QTableWidget. Decimal point.

    thank you. Following code solves my task:
    Qt Code:
    1. class DoubleDelegate: public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5.  
    6. DoubleDelegate(QObject* parent=0) : QStyledItemDelegate(parent) { }
    7. virtual ~DoubleDelegate() { }
    8.  
    9. virtual QString displayText(const QVariant &value, const QLocale &locale) const
    10. {
    11. if (value.type() == QVariant::Double) {
    12. return QString().sprintf("%.2f", value.toDouble()).replace('.',',');
    13. }
    14. return QStyledItemDelegate::displayText(value, locale);
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QAbstractItemModel, QTableWidget. Decimal point.

    If you want your code to show properly formatted doubles no matter what locale, then you should do this:

    Qt Code:
    1. class DoubleDelegate: public QStyledItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5.  
    6. DoubleDelegate(QObject* parent=0) : QStyledItemDelegate(parent) { }
    7. virtual ~DoubleDelegate() { }
    8.  
    9. virtual QString displayText(const QVariant &value, const QLocale &locale) const
    10. {
    11. if (value.type() == QVariant::Double) {
    12. return locale.toString( value.toDouble(), 'f', 2 );
    13. }
    14. return QStyledItemDelegate::displayText(value, locale);
    15. }
    16. };
    To copy to clipboard, switch view to plain text mode 

    Your original code will substitute a comma for a period in all cases, regardless of locale. Maybe that's what you want to do, but an American will certainly be confused seeing a table where the entries are all of the form "123,45".

Similar Threads

  1. Replies: 1
    Last Post: 21st December 2012, 11:00
  2. Replies: 3
    Last Post: 21st June 2011, 19:37
  3. Replies: 1
    Last Post: 3rd December 2009, 14:23
  4. QTableWidget and Decimal places
    By pshah.mumbai in forum Qt Programming
    Replies: 3
    Last Post: 17th August 2008, 08:30
  5. QPainter clipping with precision behind the decimal point
    By Pieter from Belgium in forum Qt Programming
    Replies: 0
    Last Post: 14th March 2007, 13:30

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.