2

我正在尝试将数字转换为货币字符串。我正在使用QLocale::toCurrencyString()方法。当我将系统区域设置(通过 Windows 7 区域和语言设置更改)设置为 India 并尝试使用 转换数字1872345.00QLocale().toCurrencyString(1872345.00),我得到了预期的货币字符串。

QLocale().toCurrencyString(1872345.00)

但是当我打电话时QLocale(QLocale::AnyLanguage,QLocale::AnyScript,QLocale::India).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 区域和语言设置)

4

1 回答 1

0

对我来说它看起来像一个错误;但如果您想验证系统语言环境默认使用的值,您可以尝试:

qDebug() << QLocale().languageToString(QLocale().language());
qDebug() << QLocale().scriptToString(QLocale().script());

我的将“默认”显示为脚本,但如果我这样做:

qDebug() << QLocale().script();

我得到“0”,文档说这是 QLocale::AnyScript 的值。我想说,如果你默认得到正确的东西,但在 ctor 中提供相同的语言、脚本和国家,那就有问题了。

您可能需要暂时围绕它编写代码,和/或提交错误报告。

于 2016-08-31T19:43:25.110 回答