ValidatorsIn this example you see how to write and use an own validator. Header file of the validator: /**************************************************************************** ** $Id: //depot/qt/main/examples/validator/motor.h#3 $ ** ** 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: //depot/qt/main/examples/validator/motor.cpp#3 $ ** ** 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 Header file of the mainwidget: /**************************************************************************** ** $Id: //depot/qt/main/examples/validator/vw.h#2 $ ** ** 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: //depot/qt/main/examples/validator/vw.cpp#2 $ ** ** 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 Main: /**************************************************************************** ** $Id: //depot/qt/main/examples/validator/main.cpp#2 $ ** ** 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 < |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 2.3 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com