3

I have a QLineEdit with a QDoubleValidator, calling QDoubleValidator::setRange will not validate the current text of the QLineEdit.
How could I programmatically validate (focusing and unfocusing it with the mouse work )

Here is the code of my inherited QDoubleValidator

DoubleValidator::DoubleValidator( QObject* parent ) : 
    Superclass( parent )
{    
}

DoubleValidator::~DoubleValidator()
{
}

void DoubleValidator::fixup ( QString & input ) const
{
    if( input.toDouble() > top() )
    {   
        input = QString::number( top() , 'f' );
    }   
    else if( input.toDouble() < bottom() )
    {   
        input = QString::number( bottom() , 'f' );
    }   
}

And the code of my inherited QLineEdit :

DoubleLineEdit::DoubleLineEdit( QWidget* parent ) :
   Superclass( parent )
{
   _validator =  new DoubleValidator( this );
  this->setValidator( _validator );
}


DoubleLineEdit::~DoubleLineEdit()
{
}

void DoubleLineEdit::setRange( double min, double max )
{
    _validator->setRange( min, max, 1000 );
    validate();
}

void DoubleLineEdit::setTop( double top )
{
    _validator->setTop( top );
    validate();
}    

void DoubleLineEdit::setBottom( double bottom )
{
    _validator->setBottom( bottom );
    validate();
}

void DoubleLineEdit::validate()
{
    if( !hasAcceptableInput() )
    {
        cout<<"Validation needed"<<endl;
    }
}

When I call DoubleLineEdit::setRange(), the current text of the DoubleLineEdit is not validated and fixed.

DoubleLineEdit* edit = new DoubleLineEdit( this );
edit->setText("100");
edit->setRange( 0, 10);

With this code, edit->text() will still be 100, I would like it to change automatically to 10.

I've implemented a working DoubleLineEdit::validate method:

void DoubleLineEdit::validate()
{
    if( !hasAcceptableInput() )
    {
        QFocusEvent* e = new QFocusEvent( QEvent::FocusOut );
        this->focusOutEvent( e );
        delete e;
    }
}

But it is more of a trick, and there is maybe a better solution.

AAEM
  • 1,837
  • 2
  • 18
  • 26
Mathieu Westphal
  • 2,544
  • 1
  • 19
  • 33
  • 1
    What do you mean? `QDoubleValidator::setRange` is programmatic validation. I am not sure I understand the focus / non-focus part either. – László Papp Jun 26 '14 at 09:56
  • if i call `QDoubleValidator::setRange` with a lower top, the QLineEdit will not call `QDoubleValidator::fixUp`. However focusing and unfocusing the QLineEdit will call it. So far the best way to do it is to call `QLineEdit::focusOutEvent` – Mathieu Westphal Jun 26 '14 at 10:03
  • Your question is unclear, please post your code. – RobbieE Jun 26 '14 at 10:05
  • What is "lower-top"? Can you summarize in one sentence what you are trying to achieve? – László Papp Jun 26 '14 at 10:09
  • I want my the QLineEdit to validate and fixUp his text when his QDoubleValidator::setRange is called. – Mathieu Westphal Jun 26 '14 at 10:10
  • What you are saying is still not making sense. Post your code, tell us what is actually happening and then also what you expect to happen. – RobbieE Jun 26 '14 at 10:12

1 Answers1

2

Try changing your validate() function to:

void DoubleLineEdit::validate()
{
   if (!hasAcceptableInput())
   {
       QString t = text();
       _validator->fixUp(t);
       setText(t);
   }
}
RobbieE
  • 4,280
  • 3
  • 22
  • 36