QSpinBoxを拡張して、有効な値として「NaN」または「nan」を入力できるようにしようとしています。ドキュメントによると、これを実現するにはtextFromValue、valueFromText、validate関数を使用する必要がありますが、数値以外のテキストを入力できないため、機能させることができません。これが私の.hファイルと.cppファイルにあるものです:
CPPファイル:
#include "CustomIntSpinBox.h"
CustomIntSpinBox::CustomIntSpinBox(QWidget *parent) : QSpinBox(parent)
{
this->setRange(-32767,32767);
}
QString CustomIntSpinBox::textFromValue(int value) const
{
if (value == NAN_VALUE)
{
return QString::fromStdString("nan");
}
else
{
return QString::number(value);
}
}
int CustomIntSpinBox::valueFromText(const QString &text) const
{
if (text.toLower() == QString::fromStdString("nan"))
{
return NAN_VALUE;
}
else
{
return text.toInt();
}
}
QValidator::State validate(QString &input, int pos)
{
return QValidator::Acceptable;
}
Hファイル:
#ifndef CUSTOMINTSPINBOX_H
#define CUSTOMINTSPINBOX_H
#include <QSpinBox>
#include <QWidget>
#include <QtGui>
#include <iostream>
using namespace std;
#define NAN_VALUE 32767
class CustomIntSpinBox : public QSpinBox
{
Q_OBJECT
public:
CustomIntSpinBox(QWidget *parent = 0);
virtual ~CustomIntSpinBox() throw() {}
int valueFromText(const QString &text) const;
QString textFromValue(int value) const;
QValidator::State validate(QString &input, int pos);
};
#endif // CUSTOMINTSPINBOX_H
何か足りないものはありますか?または間違っていますか?これを行うためのより簡単な方法があれば、それも知っておくとよいでしょう...