Search code examples
c++qtqwtscientific-notation

Qwt turn off scientific notation for axis labels


By default, Qwt displays large numbers on the axis in scientific notation:

axis labels in scientific notation

For my application, I'd really like to turn this off OR reformat the labels. Looking through the class documentation, it doesn't seem like any of the QwtScale classes have an option for this. Can this behavior be implemented by deriving a new class? If so, which class should it be derived from and which members would need to be overloaded?


Solution

  • Thanks to bkausbk, I was able to come up with this modified QwtScaleDraw:

    class QScaleDraw : public QwtScaleDraw
    {
    public:
    
        explicit QScaleDraw(bool enableScientificNotation = false)
        : m_scientificNotationEnabled(enableScientificNotation)
        {
    
        }
    
        virtual QwtText label(double value) const override
        {
            if (m_scientificNotationEnabled)
            {
                return QwtScaleDraw::label(value);
            } 
            else
            {
                return QwtText(QString::number(value, 'f', 0));
            }
        }
    
    private:
    
        bool    m_scientificNotationEnabled;                                                
    
    };
    

    then to use it, you do something like:

    QwtPlot myplot;
    myplot->setAxisScaleDraw(xBottom, new QScaleDraw);
    

    Result

    axis labels without scientific notation