QCustomPlot 轴中的自定义格式数字

Custom formatting numbers in axes of QCustomPlot

我想在 QCustomPlot 的轴上设置数字格式。我知道如何处理小数精度,但不知道如何在大数字的情况下使用空格而不是逗号。

我希望数字看起来像这样:

1.045   (decimals separated with a dot)
1 000  (thousands separated with space, currently I get 1,000)

有个方法QCPAxis::setNumberFormat,好像不是我要找的

您需要继承 QCPAxisTicker 并重新实现 getTickLabel 方法

我找不到将空格作为组分隔符并将点作为小数点的语言环境,所以我改为使用 QString 替换函数来制作 "custom separators".

快速粗略的示例:

QString getTickLabel (double tick, const QLocale &locale, QChar formatChar, int precision) {
    QLocale l;
    QString number = l.toString(tick, 'g', 15);
    number.replace(l.decimalPoint(), ".");
    number.replace(l.groupSeparator(), " ");

    return number;
}

输入:

1000000.1411

输出:

"1 000 000.1411"