Search code examples
c++qtqstringqtguiqlabel

Displaying a float as a QString on a QLabel displays a "G" where a dot "." should go


I'm trying to display a float in a QLabel but for some reason it keeps displaying a "G" right where the dot should go. In other words if the result is 1.23445 it displays 1G23445.

Please look at the following code...

float myFloat = 2.09863591;
QString floatAsString = QString::number(myFloat, 'f',8);
qDebug()<< "Number as String: "<< floatAsString;// here it displays 2.09863591
ui->label->setText(floatAsString);// here it displays 2G09863591

Why is that when I display the float result on a QLabel it outputs a "G" instead of a "."?

What am I missing?

Thanks


Solution

  • Your code works fine for me. You are probably doing something wrong, like not rebuilt properly, etc.

    main.cpp

    #include <QLabel>
    #include <QApplication>
    
    #include <QString>
    #include <QDebug>
    
    int main(int argc, char **argv)
    {
        QApplication a(argc, argv);
        float f = 2.09863591;
        QString s = QString::number(f, 'f', 8);
        qDebug()<< "Test:" << s;
        QLabel label;
        label.setText(s);
        label.show();
        return a.exec();
    }
    

    main.pro

    TEMPLATE = app
    TARGET = main
    greaterThan(QT_MAJOR_VERSION, 4):QT += widgets
    SOURCES += main.cpp
    

    Output

    Number as String: "2.0986359"