Search code examples
c++qtnumber-formatting

Can you set the qDebug() floating point precision and number format globally?


I want to use qDebug(), qInfo() and so on with a custom default floating point precision and number format.

Is there a way to define this globally?

Imagine this:

double num = 1.2;
qDebug() << "My floating point Number is: " << QString::number(num, 'f', 2);
//Output: My floating point Number is 1.20

Now I would like to avoid QString::number(num, 'f', 2) everytime I write a number and would much rather like to use a standard percision and format.


Solution

  • You can't. qDebug(), qFatal(), etc... return instances of the class QDebug.

    The issue is that the operator QDebug::operator<<(float f) is a non virtual class member function.
    You cannot define another without getting the compile error message

    operator<< is ambiguous