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?
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?
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,
_
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).
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,
_
thank you. Following code solves my task:Qt Code:
class DoubleDelegate: public QStyledItemDelegate { Q_OBJECT public: virtual ~DoubleDelegate() { } { } return QStyledItemDelegate::displayText(value, locale); } };To copy to clipboard, switch view to plain text mode
If you want your code to show properly formatted doubles no matter what locale, then you should do this:
Qt Code:
class DoubleDelegate: public QStyledItemDelegate { Q_OBJECT public: virtual ~DoubleDelegate() { } { return locale.toString( value.toDouble(), 'f', 2 ); } return QStyledItemDelegate::displayText(value, locale); } };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".
Bookmarks