Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2008
Corrections ot impl of class QDoubleLineEdit ?


Message 1 in thread

I derived the class (printed below) from QLineEdit because I needed to 
know that no invalid value can be inserted

With this implementation however "editingFinished" is not catched and I 
change characters in the string which lead to an invalid value this 
immediately means that the whole string is changed. This happes for 
example with "1,2e-04" being changed to "1,2e-" which directly is 
converted to an unwanted value by text.toDouble().

Additionaly I would like to know what you think in general about the 
implementation.

Matthias
--------------
#ifndef QDOUBLELINEEDIT_H_
#define QDOUBLELINEEDIT_H_

#include <QtGui/QLineEdit>
#include <QtCore/QChar>
#include <QtGui/QValidator>

#include <limits>
const double dMax = std::numeric_limits<double>::max();
const double dMin = std::numeric_limits<double>::min();

class QDoubleLineEdit : public QLineEdit
{
    Q_OBJECT

public:
    QDoubleLineEdit(QWidget* parent /*= 0*/) : QLineEdit(parent)
    {
        m_isValueFromText = false;

        setDecimals(2);
        setMaximum(100);
        setMinimum(0);
        useMagnitudeFormatExponetial();
        setValue(0);       
       
        connect(this, SIGNAL(textEdited(const QString &)), this, 
SLOT(setValueFromText (const QString &)));
        //connect(this, SIGNAL(editingFinished()), this, 
SLOT(checkValue()));
    }

    virtual ~QDoubleLineEdit(){}

private:
    int m_decimals;
    double m_maximum;
    double m_minimum;
    double m_value;
    QChar m_ExpFormat;
    bool m_isMagnitudeFormatExponetial;
    bool m_isMagnitudeFormatLinear;
    bool m_isValueFromText;

public:
    int decimals ()
    {
        return m_decimals;
    }

    void setDecimals( int prec )
    {
        if (prec < 0)
            prec = 0;
        if (prec > 16)
            prec = 16;
        m_decimals = prec;

        QRegExp regExp("[0-9]{1,6}.[0-9]{1,m_decimals}");
        setValidator(new QRegExpValidator(regExp, this));
    }

    double maximum ()
    {
        return m_maximum;
    }

    void setMaximum ( double max )
    {
        double sgn = (max > 0 ? 1 : (max < 0 ? -1 : 0));
        max = sgn * max;
        if (max > dMax) max = dMax;
        m_maximum = sgn * max;
    }

    double minimum ()
    {
        return m_minimum;
    }

    void setMinimum ( double min )
    {
        double sgn = (min > 0 ? 1 : (min < 0 ? -1 : 0));
        min= sgn * min;   

        if (min != 0)
        {
            if (min < dMin) min = dMin;
        }
        m_minimum = min * sgn;
    }

    double value ()
    {
        return m_value;
    }

    void setValue ( double val )
    {
        bool isValueChanged = false;
        if (val != m_value)
        {
            if (val < m_minimum)
            {
                val = m_minimum;
                isValueChanged = true;
            }
            if (val > m_maximum)
            {
                val = m_maximum;
                isValueChanged = true;       
            }
            m_value = val;       
        }
        // do not print new value if it is valid.
        // only if value has been set from code
        if ((!m_isValueFromText) || (m_isValueFromText && isValueChanged))
        {
            m_isValueFromText = false;
            setTextFromValue();
        }
        m_isValueFromText = false;
    }

    void setRange ( double min, double max )
    {
        setMaximum ( max );
        setMinimum ( min );
    }

    void setRangeMax()
    {
        setMaximum ( dMax );
        setMinimum ( -dMax );
    }


    void useMagnitudeFormatExponetial ()
    {
        m_isMagnitudeFormatExponetial = true;
        //setTextFromValue ();
    }

    void useMagnitudeFormatLinear ()
    {
        m_isMagnitudeFormatExponetial = false;
        //setTextFromValue ();
    }

    void reprint()
    {
        setTextFromValue ();
    }
private slots:
    void setValueFromText ( const QString & text )
    {
        m_isValueFromText = true;   
        setValue(text.toDouble());
        emit valueChanged();
    }

    void checkValue()
    {
        m_isValueFromText = true;
        setValue(text().toDouble());
    }

    void setTextFromValue ()
    {
        QString str;
        if (m_isMagnitudeFormatExponetial)
        {
            str = QString("%1").arg( m_value , 0, 'e' , m_decimals );
        }
        else
        {
            str = QString("%1").arg( m_value , 0, 'f' , m_decimals );
        }

        if (str != text())
        {
            setText(str);
        }
    }

signals:
    void valueChanged();
};
#endif


--
 [ signature omitted ]