0

I subclassed QDoubleSpinBox in order to make a QDoubleSpinBox that allows the user to enter NaN as valid input. Right now if a user enters "nan" into the spinbox the control is automatically changing the text to the value of DBL_MAX instead of staying as nan. Before i started using the math.h library with the nan and isnan functions i just defined NAN_VALUE to be 1000 and the range to be -1000 to 1000. Then in my textFromValue i checked if value was equal to NAN_VALUE. Likewise in the valueFromText function i was returning NAN_VALUE. When i did it that way it worked but i would like to be able to use the nan and isnan functions instead. Now that i added in the nan and isnan function calls it stopped working. Does anyone know why that is? Also, i noticed i had this problem with my earlier implementation when i used DBL_MIN and DBL_MAX as the range. Are those numbers too big of a range for the control? If i made the range smaller like -1000 and 1000 it worked fine..

Here is my implementation:

CustomDoubleSpinBox.h

#ifndef CUSTOMDOUBLESPINBOX_H
#define CUSTOMDOUBLESPINBOX_H

#include <QDoubleSpinBox>
#include <QWidget>
#include <QtGui>
#include <iostream>
#include <math.h>
#include <float.h>
#include <limits>

#define NUMBER_OF_DECIMALS 2

using namespace std;

class CustomDoubleSpinBox : public QDoubleSpinBox
{
    Q_OBJECT

    public:
    CustomDoubleSpinBox(QWidget *parent = 0);
    virtual ~CustomDoubleSpinBox() throw() {}

    double valueFromText(const QString &text) const;
    QString textFromValue(double value) const;
    QValidator::State validate ( QString & input, int & pos ) const;
};

#endif // CUSTOMDOUBLESPINBOX_H

CustomDoubleSpinBox.cpp

#include "CustomDoubleSpinBox.h"

CustomDoubleSpinBox::CustomDoubleSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
{
    this->setRange(DBL_MIN, DBL_MAX);
    this->setDecimals(NUMBER_OF_DECIMALS);
}

QString CustomDoubleSpinBox::textFromValue(double value) const
{
    if (isnan(value))
    {
        return QString::fromStdString("NaN");
    }
    else
    {
        QString result;
        return result.setNum(value,'f', NUMBER_OF_DECIMALS);
    }
}

double CustomDoubleSpinBox::valueFromText(const QString &text) const
{
    if (text.toLower() == QString::fromStdString("nan"))
    {
        return nan("");
    }
    else
    {
        return text.toDouble();
    }
}

QValidator::State CustomDoubleSpinBox::validate ( QString & input, int & pos ) const
{
    Q_UNUSED(input);
    Q_UNUSED(pos);

    return QValidator::Acceptable;
}
MBU
  • 4,998
  • 12
  • 59
  • 98
  • 1
    I'm curious. What is preventing you from using [Special Value Text](http://qt-project.org/doc/qt-4.8/qabstractspinbox.html#specialValueText-prop)? Never mind, the special value text will not allow the user to type in Nan, it would only display it for the selected value. – David D Apr 19 '12 at 02:36
  • What happens if you type inf and -inf? – emsr Apr 19 '12 at 03:35
  • since its not a valid double and its not "nan" it changes the text back to what it was previously on or 0. – MBU Apr 19 '12 at 03:37

0 Answers0