| Classes - Annotated - Tree - Functions - Home - Structure

# Validators

In this example you see how to write and use an own validator.

Header file of the validator:

    /****************************************************************************
    ** $Id: qt/examples/validator/motor.h   2.3.1   edited 2001-01-26 $
    **
    ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #ifndef MOTOR_H
    #define MOTOR_H

    #include <qvalidator.h>
    #include <qspinbox.h>

    class MotorValidator: public QValidator
    {
        Q_OBJECT
    public:
        MotorValidator( QSpinBox * parent, const char * name );
        ~MotorValidator();

        void setRange( int bottom, int top, int step );

        int bottom() { return b; }
        int top() { return t; }
        int step() { return s; }

        QValidator::State validate( QString &, int & ) const;

    private:
        int b, t, s;
    };

    #endif

Implementation of the validator:

    /****************************************************************************
    ** $Id: qt/examples/validator/motor.cpp   2.3.1   edited 2001-01-26 $
    **
    ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #include "motor.h"
    #include "qlineedit.h"
    #include "qpushbutton.h"

    MotorValidator::MotorValidator( QSpinBox * parent, const char * name )
        : QValidator( parent, name )
    {
        // just some random junk.
        b = 0;
        t = 42;
        s = 2;
    }

    MotorValidator::~MotorValidator()
    {
        // nothing.  wow. programming qt is easy.
    }

    void MotorValidator::setRange( int bottom, int top, int step )
    {
        b = bottom;
        t = top;
        s = step;
    }

    // the guts of this example: return TRUE if motorSize describes an
    // integer in the range b to t which can be described as n*s+b for
    // some integer n.

    QValidator::State MotorValidator::validate( QString & motorSize, int & ) const
    {
        bool ok;
        long int tmp = motorSize.toLong( &ok );
        if ( !ok )
            return QValidator::Invalid;
        else if ( tmp < b || tmp > t || ((tmp-b)%s) != 0 )
            return QValidator::Valid;
        else
            return QValidator::Acceptable;
    }

Header file of the mainwidget:

    /****************************************************************************
    ** $Id: qt/examples/validator/vw.h   2.3.1   edited 2001-01-26 $
    **
    ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #ifndef VW_H
    #define VW_H

    #include <qvalidator.h>
    #include <qstring.h>
    #include <qwidget.h>

    class VW: public QWidget {
        Q_OBJECT
    public:
        VW( QWidget * parent = 0, const char * name = 0 );
        ~VW();

    private slots:
        void modelSelected( const QString& );
        void motorSelected( int );
        void yearSelected( int );

    signals:
        void validSelectionMade( const QString& );

    private:
        void computeSelection();

        QString currentModel;
        int currentMotorSize;
        int currentYear;
    };

    #endif

Implementation of the mainwidget:

    /****************************************************************************
    ** $Id: qt/examples/validator/vw.cpp   2.3.1   edited 2001-01-26 $
    **
    ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #include "vw.h"

    #include <qlineedit.h>
    #include <qcombobox.h>
    #include <qspinbox.h>
    #include <qlayout.h>
    #include <qgroupbox.h>
    #include <qlabel.h>

    #include "motor.h"

    VW::VW( QWidget * parent, const char * name )
        : QWidget( parent, name )
    {
        QHBoxLayout * hb;

        hb = new QHBoxLayout( this, 10 );

        QGroupBox * box;
        box = new QGroupBox( this, "input box" );
        hb->addWidget( box, 0, AlignTop );

        QVBoxLayout * b;

        // set up the input box
        b = new QVBoxLayout( box, 12 );

        QLabel * l = new QLabel( "Enter Vehicle Details", box, "header" );
        l->setMinimumSize( l->sizeHint() );
        b->addWidget( l );
        QFrame * f = new QFrame( box, "horizontal divider" );
        f->setFrameStyle( QFrame::HLine | QFrame::Sunken );
        f->setMinimumHeight( 12 );
        b->addWidget( f );

        QGridLayout *grid = new QGridLayout( 3, 2 );
        b->addLayout( grid );

        // here we start on the input grid, with labels and other widget
        // neatly arranged. the variable names are reused all over the
        // place.

        QComboBox * model = new QComboBox( FALSE, box, "model selection" );
        model->insertItem( "Type 1 Beetle" );
        model->insertItem( "Camper" );
        model->insertItem( "Van" );
        model->insertItem( "Fastback" );
        model->insertItem( "Squareback" );
        model->insertItem( "Notchback" );
        model->insertItem( "411" );
        model->setCurrentItem( model->count() - 1 ); // I like the 411
        currentModel = "411";
        model->insertItem( "412" );
        model->insertItem( "Karmann Ghia" );
        model->insertItem( "Thing" );
        model->insertItem( "Safari" );
        model->insertItem( "Kubelwagen" );
        model->insertItem( "Trekker" );
        model->insertItem( "Baja" );
        model->setMinimumSize( model->sizeHint() );
        model->setMaximumHeight( model->minimumSize().height() );
        grid->addWidget( model, 0, 1 );

        l = new QLabel( model, "Model:", box, "model label" );
        l->setMinimumSize( l->sizeHint() );
        grid->addWidget( l, 0, 0 );

        QSpinBox * motor = new QSpinBox( 1000, 1600, 100,
                                         box, "motor size selection" );
        motor->setValue( 1000 );
        currentMotorSize = 1000;
        motor->setMinimumSize( motor->sizeHint() );
        motor->setMaximumHeight( motor->minimumSize().height() );
        grid->addWidget( motor, 1, 1 );

        l = new QLabel( motor, "Motor size (cc):", box, "motor size label" );
        l->setMinimumSize( l->sizeHint() );
        grid->addWidget( l, 1, 0 );

        QSpinBox * year = new QSpinBox( box, "model year" );
        year->setRange( 1949, 1981 );
        year->setValue( 1949 );
        currentYear = 1949;
        year->setMinimumSize( year->sizeHint() );
        year->setMaximumHeight( year->minimumSize().height() );
        grid->addWidget( year, 2, 1 );

        l = new QLabel( year, "Year:", box, "model year label" );
        l->setMinimumSize( l->sizeHint() );
        grid->addWidget( l, 2, 0 );

        b->addStretch( 1 );

        b->activate();

        // output box

        box = new QGroupBox( this, "output box" );
        hb->addWidget( box, 0 );

        b = new QVBoxLayout( box, 12 );

        l = new QLabel( "Resulting Limousine:", box, "header" );
        l->setMinimumSize( l->sizeHint() );
        b->addWidget( l );

        f = new QFrame( box, "horizontal divider" );
        f->setFrameStyle( QFrame::HLine | QFrame::Sunken );
        f->setMinimumHeight( 12 );
        b->addWidget( f );

        l = new QLabel( box, "output label" );
        l->setAlignment( AlignTop | AlignLeft | WordBreak );
        l->setText( "No VW selected yet." );
        b->addWidget( l, 1 );

        b->addStretch( 1 );

        b->activate();
        hb->activate();

        // set up connections
        connect( model, SIGNAL(activated(const QString&)),
                 this, SLOT(modelSelected(const QString&)) );
        connect( motor, SIGNAL(valueChanged(int)),
                 this, SLOT(motorSelected(int)) );
        connect( year, SIGNAL(valueChanged(int)),
                 this, SLOT(yearSelected(int)) );

        connect( this, SIGNAL(validSelectionMade(const QString&)),
                 l, SLOT(setText(const QString&)) );
    }

    VW::~VW()
    {
        // nothing needs to be done.
    }

    void VW::modelSelected( const QString& m )
    {
        currentModel = m;
        computeSelection();
    }

    void VW::motorSelected( int m )
    {
        currentMotorSize = m;
        computeSelection();
    }

    void VW::yearSelected( int y )
    {
        currentYear = y;
        computeSelection();
    }

    void VW::computeSelection()
    {
        if ( currentModel.isNull() )
            return; // no model selected yet

        QString s;
        s.sprintf( "You have selected a Wolkswagen %s model %d with a "
                   "%d cm³ motor.\n\nGood choice!",
                   (const char *)currentModel,
                   (int)currentYear, currentMotorSize );
        emit validSelectionMade( s );
    }

Main:

    /****************************************************************************
    ** $Id: qt/examples/validator/main.cpp   2.3.1   edited 2001-01-26 $
    **
    ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
    **
    ** This file is part of an example program for Qt.  This example
    ** program may be used, distributed and modified without limitation.
    **
    *****************************************************************************/

    #include <qapplication.h>
    #include "vw.h"

    int main( int argc, char ** argv ) {
        QApplication a( argc, argv );
        VW mw;
        a.setMainWidget( &mw );
        mw.setCaption( "" );
        mw.show();
        return a.exec();
    }