📄 validator-main-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - validator/main.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Validators</h1><br clear="all"> In this example you see how to write and use an own validator. <hr> Header file of the validator: <pre>/****************************************************************************** $Id: qt/examples/validator/motor.h 2.3.8 edited 2004-05-12 $**** 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 <<a href="qvalidator-h.html">qvalidator.h</a>>#include <<a href="qspinbox-h.html">qspinbox.h</a>>class MotorValidator: public QValidator{ Q_OBJECTpublic: MotorValidator( <a href="qspinbox.html">QSpinBox</a> * 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; } <a href="qvalidator.html#State">QValidator::State</a> validate( <a href="qstring.html">QString</a> &, int & ) const;private: int b, t, s;};#endif</pre> <hr> Implementation of the validator: <pre>/****************************************************************************** $Id: qt/examples/validator/motor.cpp 2.3.8 edited 2004-05-12 $**** 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 "<a href="qlineedit-h.html">qlineedit.h</a>"#include "<a href="qpushbutton-h.html">qpushbutton.h</a>"MotorValidator::MotorValidator( <a href="qspinbox.html">QSpinBox</a> * parent, const char * name ) : <a href="qvalidator.html">QValidator</a>( parent, name ){ // just some random junk. b = 0; t = 42; s = 2;}MotorValidator::~MotorValidator(){ // nothing. wow. programming qt is easy.}void <a name="37"></a>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.<a href="qvalidator.html#State">QValidator::State</a> <a name="38"></a>MotorValidator::validate( <a href="qstring.html">QString</a> & 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;}</pre> <hr> Header file of the mainwidget: <pre>/****************************************************************************** $Id: qt/examples/validator/vw.h 2.3.8 edited 2004-05-12 $**** 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 <<a href="qvalidator-h.html">qvalidator.h</a>>#include <<a href="qstring-h.html">qstring.h</a>>#include <<a href="qwidget-h.html">qwidget.h</a>>class VW: public QWidget { Q_OBJECTpublic: VW( <a href="qwidget.html">QWidget</a> * 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(); <a href="qstring.html">QString</a> currentModel; int currentMotorSize; int currentYear;};#endif</pre> <hr> Implementation of the mainwidget: <pre>/****************************************************************************** $Id: qt/examples/validator/vw.cpp 2.3.8 edited 2004-05-12 $**** 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 <<a href="qlineedit-h.html">qlineedit.h</a>>#include <<a href="qcombobox-h.html">qcombobox.h</a>>#include <<a href="qspinbox-h.html">qspinbox.h</a>>#include <<a href="qlayout-h.html">qlayout.h</a>>#include <<a href="qgroupbox-h.html">qgroupbox.h</a>>#include <<a href="qlabel-h.html">qlabel.h</a>>#include "motor.h"VW::VW( <a href="qwidget.html">QWidget</a> * parent, const char * name ) : <a href="qwidget.html">QWidget</a>( parent, name ){ <a href="qhboxlayout.html">QHBoxLayout</a> * hb; hb = new <a href="qhboxlayout.html">QHBoxLayout</a>( this, 10 ); <a href="qgroupbox.html">QGroupBox</a> * box; box = new <a href="qgroupbox.html">QGroupBox</a>( this, "input box" ); hb-><a href="qboxlayout.html#ebba99">addWidget</a>( box, 0, AlignTop ); <a href="qvboxlayout.html">QVBoxLayout</a> * b; // set up the input box b = new <a href="qvboxlayout.html">QVBoxLayout</a>( box, 12 ); <a href="qlabel.html">QLabel</a> * l = new <a href="qlabel.html">QLabel</a>( "Enter Vehicle Details", box, "header" ); l-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( l-><a href="qlabel.html#f40fcc">sizeHint</a>() ); b-><a href="qboxlayout.html#ebba99">addWidget</a>( l ); <a href="qframe.html">QFrame</a> * f = new <a href="qframe.html">QFrame</a>( box, "horizontal divider" ); f-><a href="qframe.html#558f79">setFrameStyle</a>( QFrame::HLine | QFrame::Sunken ); f-><a href="qwidget.html#e24afa">setMinimumHeight</a>( 12 ); b-><a href="qboxlayout.html#ebba99">addWidget</a>( f ); <a href="qgridlayout.html">QGridLayout</a> *grid = new <a href="qgridlayout.html">QGridLayout</a>( 3, 2 ); b-><a href="qboxlayout.html#6ff301">addLayout</a>( grid ); // here we start on the input grid, with labels and other widget // neatly arranged. the variable names are reused all over the // place. <a href="qcombobox.html">QComboBox</a> * model = new <a href="qcombobox.html">QComboBox</a>( FALSE, box, "model selection" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Type 1 Beetle" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Camper" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Van" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Fastback" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Squareback" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Notchback" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "411" ); model-><a href="qcombobox.html#8a94f7">setCurrentItem</a>( model-><a href="qcombobox.html#a03e60">count</a>() - 1 ); // I like the 411 currentModel = "411"; model-><a href="qcombobox.html#613b4b">insertItem</a>( "412" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Karmann Ghia" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Thing" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Safari" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Kubelwagen" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Trekker" ); model-><a href="qcombobox.html#613b4b">insertItem</a>( "Baja" ); model-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( model-><a href="qcombobox.html#dd19df">sizeHint</a>() ); model-><a href="qwidget.html#4e5337">setMaximumHeight</a>( model-><a href="qwidget.html#b24d94">minimumSize</a>().height() ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( model, 0, 1 ); l = new <a href="qlabel.html">QLabel</a>( model, "Model:", box, "model label" ); l-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( l-><a href="qlabel.html#f40fcc">sizeHint</a>() ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( l, 0, 0 ); <a href="qspinbox.html">QSpinBox</a> * motor = new <a href="qspinbox.html">QSpinBox</a>( 1000, 1600, 100, box, "motor size selection" ); motor-><a href="qspinbox.html#eb409b">setValue</a>( 1000 ); currentMotorSize = 1000; motor-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( motor-><a href="qspinbox.html#ffd3ee">sizeHint</a>() ); motor-><a href="qwidget.html#4e5337">setMaximumHeight</a>( motor-><a href="qwidget.html#b24d94">minimumSize</a>().height() ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( motor, 1, 1 ); l = new <a href="qlabel.html">QLabel</a>( motor, "Motor size (cc):", box, "motor size label" ); l-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( l-><a href="qlabel.html#f40fcc">sizeHint</a>() ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( l, 1, 0 ); <a href="qspinbox.html">QSpinBox</a> * year = new <a href="qspinbox.html">QSpinBox</a>( box, "model year" ); year-><a href="qrangecontrol.html#f2409c">setRange</a>( 1949, 1981 ); year-><a href="qspinbox.html#eb409b">setValue</a>( 1949 ); currentYear = 1949; year-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( year-><a href="qspinbox.html#ffd3ee">sizeHint</a>() ); year-><a href="qwidget.html#4e5337">setMaximumHeight</a>( year-><a href="qwidget.html#b24d94">minimumSize</a>().height() ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( year, 2, 1 ); l = new <a href="qlabel.html">QLabel</a>( year, "Year:", box, "model year label" ); l-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( l-><a href="qlabel.html#f40fcc">sizeHint</a>() ); grid-><a href="qgridlayout.html#dac29c">addWidget</a>( l, 2, 0 ); b-><a href="qboxlayout.html#0226eb">addStretch</a>( 1 ); b-><a href="qlayout.html#1cb33d">activate</a>(); // output box box = new <a href="qgroupbox.html">QGroupBox</a>( this, "output box" ); hb-><a href="qboxlayout.html#ebba99">addWidget</a>( box, 0 ); b = new <a href="qvboxlayout.html">QVBoxLayout</a>( box, 12 ); l = new <a href="qlabel.html">QLabel</a>( "Resulting Limousine:", box, "header" ); l-><a href="qwidget.html#c0b5fb">setMinimumSize</a>( l-><a href="qlabel.html#f40fcc">sizeHint</a>() ); b-><a href="qboxlayout.html#ebba99">addWidget</a>( l ); f = new <a href="qframe.html">QFrame</a>( box, "horizontal divider" ); f-><a href="qframe.html#558f79">setFrameStyle</a>( QFrame::HLine | QFrame::Sunken ); f-><a href="qwidget.html#e24afa">setMinimumHeight</a>( 12 ); b-><a href="qboxlayout.html#ebba99">addWidget</a>( f ); l = new <a href="qlabel.html">QLabel</a>( box, "output label" ); l-><a href="qlabel.html#4743c8">setAlignment</a>( AlignTop | AlignLeft | WordBreak ); l-><a href="qlabel.html#bc5ea6">setText</a>( "No VW selected yet." ); b-><a href="qboxlayout.html#ebba99">addWidget</a>( l, 1 ); b-><a href="qboxlayout.html#0226eb">addStretch</a>( 1 ); b-><a href="qlayout.html#1cb33d">activate</a>(); hb-><a href="qlayout.html#1cb33d">activate</a>(); // set up connections <a href="qobject.html#fbde73">connect</a>( model, SIGNAL(activated(const QString&)), this, SLOT(<a href=#39>modelSelected</a>(const QString&)) ); <a href="qobject.html#fbde73">connect</a>( motor, SIGNAL(valueChanged(int)), this, SLOT(<a href=#40>motorSelected</a>(int)) ); <a href="qobject.html#fbde73">connect</a>( year, SIGNAL(valueChanged(int)), this, SLOT(<a href=#41>yearSelected</a>(int)) ); <a href="qobject.html#fbde73">connect</a>( this, SIGNAL(validSelectionMade(const QString&)), l, SLOT(setText(const QString&)) );}VW::~VW(){ // nothing needs to be done.}void <a name="39"></a>VW::modelSelected( const QString& m ){ currentModel = m; <a href=#42>computeSelection</a>();}void <a name="40"></a>VW::motorSelected( int m ){ currentMotorSize = m; <a href=#42>computeSelection</a>();}void <a name="41"></a>VW::yearSelected( int y ){ currentYear = y; <a href=#42>computeSelection</a>();}void <a name="42"></a>VW::computeSelection(){ if ( currentModel.isNull() ) return; // no model selected yet <a href="qstring.html">QString</a> s; s.<a href="qstring.html#926f67">sprintf</a>( "You have selected a Volkswagen %s model %d with a " "%d cm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -