?? qspinbox.cpp
字號:
The \a valueText parameter is the same string that is displayed in the edit field of the spin box. \sa value()*//*! Intercepts and handles those events coming to the embedded QLineEdit which have special meaning for the QSpinBox.*/bool QSpinBox::eventFilter( QObject* obj, QEvent* ev ){ if ( obj != vi ) return FALSE; if ( ev->type() == QEvent::FocusOut || ev->type() == QEvent::Hide ) { if ( edited ) interpretText();#ifdef QT_KEYPAD_MODE if( qt_modalEditingEnabled ) update(); } else if ( qt_modalEditingEnabled && ev->type() == QEvent::FocusIn ) { if ( ((QFocusEvent *)ev)->reason() != QFocusEvent::Mouse ){ vi->selectAll(); update(); } } else if ( qt_modalEditingEnabled && (ev->type() == QEvent::EnterModalEdit || ev->type() == QEvent::LeaveModalEdit)) { update();#endif } else if ( ev->type() == QEvent::KeyPress ) { QKeyEvent* k = (QKeyEvent*)ev;#ifdef QT_KEYPAD_MODE if( qt_modalEditingEnabled ) { switch( k->key() ) { case Key_Select: if ( edited ) interpretText(); if ( !vi->isModalEditing() ) { if ( !isModalEditing() ) { setModalEditing( TRUE ); } else { setModalEditing( FALSE ); return FALSE; //eg. pass it to qlineedit } }else{ update(); } break; case Key_Back: case Key_No: // if vi is the modal editor we know it will get the event after us, // so don't eat it here as it needs to restore the original value. if ( isModalEditing() ) setModalEditing( FALSE ); break; default: if ( !isModalEditing() && !vi->isModalEditing() ) return FALSE; } }#endif if ( k->key() == Key_Up ) { stepUp(); return TRUE; } else if ( k->key() == Key_Down ) { stepDown(); return TRUE; } else if ( k->key() == Key_Return ) { interpretText(); return FALSE; } } return FALSE;}/*!\reimp*/void QSpinBox::leaveEvent( QEvent* ){}/*!\reimp*/void QSpinBox::resizeEvent( QResizeEvent* ){ arrangeWidgets();}/*!\reimp*/void QSpinBox::wheelEvent( QWheelEvent * e ){ e->accept(); static float offset = 0; static QSpinBox* offset_owner = 0; if (offset_owner != this) { offset_owner = this; offset = 0; } offset += -e->delta()/120; if (QABS(offset) < 1) return; int ioff = int(offset); int i; for (i=0; i<QABS(ioff); i++) offset > 0 ? stepDown() : stepUp(); offset -= ioff;}/*! This method gets called by QRangeControl whenever the value has changed. Updates the display and emits the valueChanged() signals.*/void QSpinBox::valueChange(){ updateDisplay(); emit valueChanged( value() ); emit valueChanged( currentValueText() );}/*! This method gets called by QRangeControl whenever the range has changed. It adjusts the default validator and updates the display.*/void QSpinBox::rangeChange(){ if ( validate->inherits( "QIntValidator" ) ) ((QIntValidator*)validate)->setRange( minValue(), maxValue() ); updateDisplay();}/*! Sets the validator to \a v. The validator controls what keyboard input is accepted when the user is editing in the value field. The default is to use a suitable QIntValidator. Use setValidator(0) to turn off input validation (entered input will still be clamped to the range of the spinbox).*/void QSpinBox::setValidator( const QValidator* v ){ if ( vi ) vi->setValidator( v );}/*! Returns the validator which constrains editing for this spin box if there is any, or else 0. \sa setValidator() QValidator*/const QValidator * QSpinBox::validator() const{ return vi ? vi->validator() : 0;}/*! Updates the contents of the embedded QLineEdit to reflect current value, using mapValueToText(). Also enables/disables the push buttons accordingly. \sa mapValueToText()*/void QSpinBox::updateDisplay(){ vi->setText( currentValueText() ); edited = FALSE; up->setEnabled( isEnabled() && (wrapping() || value() < maxValue()) ); down->setEnabled( isEnabled() && (wrapping() || value() > minValue()) );}/*! QSpinBox calls this after the user has manually edited the contents of the spin box (not using the up/down buttons/keys). The default implementation of this function interprets the new text using mapTextToValue(). If mapTextToValue() is successful, it changes the spin box' value. If not the value if left unchanged.*/void QSpinBox::interpretText(){ bool ok = TRUE; bool done = FALSE; int newVal = 0; if ( !specialValueText().isEmpty() ) { QString s = QString(text()).stripWhiteSpace(); QString t = QString(specialValueText()).stripWhiteSpace(); if ( s == t ) { newVal = minValue(); done = TRUE; } } if ( !done ) newVal = mapTextToValue( &ok ); if ( ok ) setValue( newVal ); updateDisplay(); // Sometimes redundant}/*! Returns a pointer to the embedded 'up' button.*/QPushButton* QSpinBox::upButton() const{ return up;}/*! Returns a pointer to the embedded 'down' button.*/QPushButton* QSpinBox::downButton() const{ return down;}/*! Returns a pointer to the embedded QLineEdit.*/QLineEdit* QSpinBox::editor() const{ return vi;}/*! This slot gets called whenever the user edits the text of the spin box.*/void QSpinBox::textChanged(){ edited = TRUE; // This flag is cleared in updateDisplay()};/*! This virtual function is used by the spin box whenever it needs to display value \a v. The default implementation returns a string containing \a v printed in the standard way. Reimplement this function in in a subclass if you want a specialized spin box, handling something else than integers. This function need not be concerned with \link setPrefix() prefix \endlink or \link setSuffix() suffix \endlink or \link setSpecialValueText() special-value text, \endlink the QSpinBox handles that automatically. \sa updateDisplay(), mapTextToValue()*/QString QSpinBox::mapValueToText( int v ){ QString s; s.setNum( v ); return s;}/*! This virtual function is used by the spin box whenever it needs to interpret the text entered by the user as a value. The default implementation tries to interpret it as an integer in the standard way, and returns the integer value. Reimplement this function in in a subclass if you want a specialized spin box, handling something else than integers. It should call text() (or cleanText() ) and return the value corresponding to that text. If the text does not represent a legal value (uninterpretable), the bool pointed to by \a ok should be set to FALSE. This function need not be concerned with \link setSpecialValueText() special-value text, \endlink the QSpinBox handles that automatically. \sa interpretText(), mapValueToText()*/int QSpinBox::mapTextToValue( bool* ok ){ QString s = text(); int newVal = s.toInt( ok ); if ( !(*ok) && !( !prefix() && !suffix() ) ) {// Try removing any pre/suffix s = cleanText(); newVal = s.toInt( ok ); } return newVal;}/*! Returns the full text calculated from the current value, including any prefix, suffix or special-value text.*/QString QSpinBox::currentValueText(){ QString s; if ( (value() == minValue()) && !specialValueText().isEmpty() ) { s = specialValueText(); } else { s = prefix(); s.append( mapValueToText( value() ) ); s.append( suffix() ); } return s;}/*! \reimp*/void QSpinBox::setEnabled( bool on ){ bool b = isEnabled(); QFrame::setEnabled( on ); if ( isEnabled() != b ) { // ## enabledChange() might have been a better choice updateDisplay(); }}/*! \reimp */void QSpinBox::styleChange( QStyle& old ){ if ( style() == WindowsStyle ) setFrameStyle( WinPanel | Sunken ); else setFrameStyle( Panel | Sunken ); arrangeWidgets(); QWidget::styleChange( old );}/*! \enum QSpinBox::ButtonSymbols This enum type determines what the buttons in a spin box show. The currently defined values are: <ul> <li> \c UpDownArrows - the buttons show little arrows, in the classic style. This is the default. <li> \c PlusMinus - the buttons show '+' and '-' symbols. This is often considered to be more meaningful than \c UpDownArrows. </ul>*//*! Sets the spin box to display \a newSymbols on its buttons. \a newSymbols can be either \c UpDownArrows (the default) or \c PlusMinus. \sa buttonSymbols() ButtonSymbols*/void QSpinBox::setButtonSymbols( ButtonSymbols newSymbols ){ if ( buttonSymbols() == newSymbols ) return; if ( !d ) d = new QSpinBoxPrivate; d->buttonSymbols = newSymbols; updateButtonSymbols();}/*! Returns the current button symbol mode. The default is \c UpDownArrows. \sa setButtonSymbols() ButtonSymbols*/QSpinBox::ButtonSymbols QSpinBox::buttonSymbols() const{ return d ? d->buttonSymbols : UpDownArrows;}// this function uses the pixmap cache for a Different Reason: the// pixmap cache also preserves QPixmap::serialNumber(). by doing// this, QButton::setPixmap() is able to avoid flicker e.g. when the// spin box is resized in such a way that the height of the buttons// does not change (common the default size policy).void QSpinBox::updateButtonSymbols(){ QString key( QString::fromLatin1( "$qt$qspinbox$" ) ); bool pmSym = buttonSymbols() == PlusMinus; key += QString::fromLatin1( pmSym ? "+-" : "^v" ); key += QString::number( down->height() ); QString upKey = key + QString::fromLatin1( "$up" ); QString dnKey = key + QString::fromLatin1( "$down" ); QBitmap upBm; QBitmap dnBm; bool found = QPixmapCache::find( dnKey, dnBm ) && QPixmapCache::find( upKey, upBm ); if ( !found ) { QPainter p; if ( pmSym ) { int h = down->height()-4; if ( h < 3 ) return; else if ( h == 4 ) h = 3; else if ( (h > 6) && (h & 1) ) h--; h -= ( h / 8 ) * 2; // Empty border dnBm.resize( h, h ); p.begin( &dnBm ); p.eraseRect( 0, 0, h, h ); p.setBrush( color1 ); int c = h/2; p.drawLine( 0, c, h, c ); if ( !(h & 1) ) p.drawLine( 0, c-1, h, c-1 ); p.end(); upBm = dnBm; p.begin( &upBm ); p.drawLine( c, 0, c, h ); if ( !(h & 1) ) p.drawLine( c-1, 0, c-1, h ); p.end(); } else { int w = down->width()-4; if ( w < 3 ) return; else if ( !(w & 1) ) w--; w -= ( w / 7 ) * 2; // Empty border int h = w/2 + 2; // Must have empty row at foot of arrow dnBm.resize( w, h ); p.begin( &dnBm ); p.eraseRect( 0, 0, w, h ); QPointArray a; a.setPoints( 3, 0, 1, w-1, 1, h-2, h-1 ); p.setBrush( color1 ); p.drawPolygon( a ); p.end();#ifndef QT_NO_TRANSFORMATIONS QWMatrix wm; wm.scale( 1, -1 ); upBm = dnBm.xForm( wm );#else upBm.resize( w, h ); p.begin( &upBm ); p.eraseRect( 0, 0, w, h ); a.setPoints( 3, 0, h-2, w-1, h-2, h-2, 0 ); p.setBrush( color1 ); p.drawPolygon( a ); p.end();#endif } QPixmapCache::insert( dnKey, dnBm ); QPixmapCache::insert( upKey, upBm ); } down->setPixmap( dnBm ); up->setPixmap( upBm );}/*! \reimp*/int QSpinBox::minValue() const{ return QRangeControl::minValue();}/*! \reimp*/int QSpinBox::maxValue() const{ return QRangeControl::maxValue();}/*! A convenience function which just calls setRange( i, maxValue() ) \sa setRange()*/void QSpinBox::setMinValue( int i ){ setRange( i, maxValue() );}/*! A convenience function which just calls setRange( minValue(), i ) \sa setRange()*/void QSpinBox::setMaxValue( int i ){ setRange( minValue(), i );}/*! \reimp*/int QSpinBox::lineStep() const{ return QRangeControl::lineStep();}/*! Sets the line step to \e i. Calls the virtual stepChange() function if the new line step is different from the previous setting. \sa lineStep() QRangeControl::setSteps() setRange()*/void QSpinBox::setLineStep( int i ){ setSteps( i, pageStep() );}/*! \reimp*/int QSpinBox::value() const{ if ( edited ) { QSpinBox *that = (QSpinBox*)this; that->edited = FALSE; that->interpretText(); } return QRangeControl::value();}#endif
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -