2012-04-12 12 views
2

"NaN"または "nan"を有効な値として入力できるようにQSpinBoxを拡張しようとしています。ドキュメントによると、私はtextFromValue、valueFromText、およびこれを達成するための関数を検証する必要がありますが、私はそれがまだ数字以外のテキストを入力することを許可して以来、それを働かせることができません。ここで私は私の.hと.cppのファイルの中に持っているものです。QSpinBoxは有効な値としてNaNを入力します

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 

が不足している何かイムありますか?または間違っている?これを行う簡単なやり方であれば、知っていると素晴らしいかもしれません...

+0

いくつかの提案、質問とは独立して:ここでは、コンパイル例です。//:あなたがしなければならない場合を除き、基本クラスは、([サッター/アレキ](HTTPないので、(1)スロー仕様を使用しないでください。 www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586)、Item 75)。 (2)あなたのctorを明示的にする(同上、項目40)。 (3)ヘッダーに 'using namespace'を書かないでください(同上、項目59)。 (4) '#define'の代わりに' static const int NAN_VALUE = 32767; 'を使います(同上、項目16)。 (5) '#include '(コンパイルが遅くなります)をしないでください。 (6) 'QString :: fromStdString(" nan ")'(より速い)の代わりに 'QLatin1String(" nan ")'を使います。 –

答えて

4

QAbstractSpinBox::validate()のシグネチャは次のとおりです。

QValidator::State QAbstractSpinBox::validate (QString & input, int & pos) const 

だからあなたvalidate()メソッドのシグネチャは、次の2つの方法で異なる:そのないconst、あなたはint posの代わりint& posを持っています。したがって、QAbstractSpinBox::validateを上書きせず、QAbstractSpinBoxによって決して呼び出されません。

+0

ありがとうございました!それが問題でした。 – MBU

+0

QDoubleSpinBoxで同じことをしたいのであれば、このコードはまだ動作しますか?または、テキストの小数点を考慮して関数を変更する必要がありますか? – MBU

1

多分QSpinBoxはQValidatorとしてQIntValidatorを持つlineEditを設定します。少なくともQAbstractSpinBox :: setLineEditのドキュメントは、lineEditのバリデータがQAbstractSpinBox :: validate関数よりも優先されることを示唆しています。

2

あなたは、文字列nanvalue() == minimum()が表示されますされ、下で-1あなたの範囲の下限と通常のQSpinBoxまたはQDoubleSpinBoxを使用してspinbox->specialValueText("nan")を呼び出すを拡張することができます。ユーザーは文字列 "nan"を手動で入力することはできませんが、spinbox->setValue(spinbox->minimum())を実行するボタンを使用してスピンボックスにいつでも同行することができます。あなたのコードに関する

// main.cpp 
#include <QSpinBox> 
#include <QDoubleSpinBox> 
#include <QDialog> 
#include <QApplication> 
#include <QVBoxLayout> 

#include <limits> 

class Tester : public QDialog { 
    Q_OBJECT 
public: 
    static const int NanValue = -32767-1; 
    explicit Tester(QWidget * parent=0) 
     : QDialog(parent), 
      m_spinBox(new QSpinBox(this)), 
      m_doubleSpinBox(new QDoubleSpinBox(this)) 
    { 
     QVBoxLayout * vlay = new QVBoxLayout(this); 
     vlay->addWidget(m_spinBox); 
     vlay->addWidget(m_doubleSpinBox); 

     m_spinBox->setRange(NanValue,32767); 
     m_doubleSpinBox->setRange(NanValue,32767); 

     m_spinBox->setValue(NanValue); 
     m_doubleSpinBox->setValue(NanValue); 

     updateSpecialValueText(); 
    } 

protected: 
    void changeEvent(QEvent *e) { 
     QDialog::changeEvent(e); 
     if (e->type() == QEvent::LocaleChange) 
      updateSpecialValueText(); 
    } 


private: 
    void updateSpecialValueText() { 
     static const double NaN = std::numeric_limits<double>::quiet_NaN(); 
     m_spinBox->setSpecialValueText(locale().toString(NaN)); 
     m_doubleSpinBox->setSpecialValueText(locale().toString(NaN)); 
    } 

private: 
    QSpinBox * m_spinBox; 
    QDoubleSpinBox * m_doubleSpinBox; 
}; 

int main(int argc, char * argv[]) { 
    QApplication app(argc, argv); 

    Tester t; 
    return t.exec(); 
} 

#include "main.moc" 
関連する問題