数値を通貨文字列に変換しようとしています。QLocale::toCurrencyString()
メソッドを使用しています。1872345.00
システム ロケール (Windows 7 の地域と言語の設定で変更) をインドとして設定し、を使用して数値を変換しようとするとQLocale().toCurrencyString(1872345.00)
、期待される通貨文字列が得られます。
しかし、 を呼び出すとQLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00);
、このようにフォーマットされた番号が表示されますが、これは正しくありません。
異なるロケール スクリプトとロケール言語を選択してみました。しかし、正しい結果を得ることができませんでした。Qt 5.1.0 を使用しています。使用すると正しい結果が得QLocale().toCurrencyString()
られますが、システムロケールで設定された国だけでなく、複数の国の通貨を扱っているため、使用できません。この間違った結果が得られるのはなぜですか? 数値を (異なる国の) 通貨文字列に変換するにはどうすればよいですか?
コード例を次に示します。
//CurrencyForms.h
#ifndef CURRENCYFORMS_H
#define CURRENCYFORMS_H
#include <QObject>
class CurrencyForms : public QObject
{
Q_OBJECT
Q_PROPERTY(QString defaultLocaleString READ defaultLocaleString CONSTANT)
Q_PROPERTY(QString constructedLocaleStringAnyLanguage READ constructedLocaleStringAnyLanguage CONSTANT)
Q_PROPERTY(QString constructedLocaleStringHindiLanguage READ constructedLocaleStringHindiLanguage CONSTANT)
public:
explicit CurrencyForms(QObject *parent = 0);
QString defaultLocaleString();
QString constructedLocaleStringAnyLanguage();
QString constructedLocaleStringHindiLanguage();
};
#endif // CURRENCYFORMS_H
//CurrencyForms.cpp
#include "CurrencyForms.h"
#include<QLocale>
CurrencyForms::CurrencyForms(QObject *parent) :
QObject(parent)
{
}
QString CurrencyForms::defaultLocaleString()
{
return QLocale().toCurrencyString(1872345.00);
}
QString CurrencyForms::constructedLocaleStringAnyLanguage()
{
return QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00);
}
QString CurrencyForms::constructedLocaleStringHindiLanguage()
{
return QLocale(QLocale::Hindi,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00);
}
//main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>
#include <CurrencyForms.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
CurrencyForms forms;
viewer.rootContext()->setContextProperty("forms",&forms);
viewer.setMainQmlFile(QStringLiteral("qml/CurrencySymbolTest/main.qml"));
viewer.showExpanded();
return app.exec();
}
//main.qml
import QtQuick 2.0
Rectangle {
width: 720
height: 360
Grid{
columns:1
spacing:2
width: parent.width
Text{
id:defaultLocaleText
width:parent.width
height:60
font.pixelSize:14
elide:Text.ElideRight
text:"QLocale().toCurrencyString(1872345.00)"
verticalAlignment: Text.AlignVCenter
}
Text{
id:defaultLocaleTextResult
width:parent.width
height:60
font.pixelSize:40
elide:Text.ElideRight
text:forms.defaultLocaleString
verticalAlignment: Text.AlignVCenter
color:"green"
}
Text{
id:constructedLocaleStringAnyLanguageText
width:parent.width
height:60
font.pixelSize:14
elide:Text.ElideRight
text:"QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00)"
verticalAlignment: Text.AlignVCenter
}
Text{
id:constructedLocaleStringAnyResult
width:parent.width
height:60
font.pixelSize:40
elide:Text.ElideRight
text:forms.constructedLocaleStringAnyLanguage
verticalAlignment: Text.AlignVCenter
color:"red"
}
Text{
id:constructedLocaleStringHindiLanguage
width:parent.width
height:60
font.pixelSize:14
elide:Text.ElideRight
text:"QLocale(QLocale::Hindi,QLocale::AnyScript,QLocale::India).toCurrencyString(1872345.00)"
verticalAlignment: Text.AlignVCenter
}
Text{
id:constructedLocaleStringHindiLanguageResult
width:parent.width
height:60
font.pixelSize:40
elide:Text.ElideRight
text:forms.constructedLocaleStringHindiLanguage
verticalAlignment: Text.AlignVCenter
color:"red"
}
}
}
//結果
ここで、デフォルトのシステム ロケールはヒンディー語インドに設定されています (Windows 7 の地域と言語の設定による)。