Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QSpinBox Class

TheQSpinBox class provides a spin box widget.More...

Header:#include <QSpinBox>
Inherits:QAbstractSpinBox

Properties

Public Functions

QSpinBox(QWidget * parent = 0)
QStringcleanText() const
intmaximum() const
intminimum() const
QStringprefix() const
voidsetMaximum(int max)
voidsetMinimum(int min)
voidsetPrefix(const QString & prefix)
voidsetRange(int minimum, int maximum)
voidsetSingleStep(int val)
voidsetSuffix(const QString & suffix)
intsingleStep() const
QStringsuffix() const
intvalue() const

Public Slots

voidsetValue(int val)

Signals

voidvalueChanged(int i)
voidvalueChanged(const QString & text)

Protected Functions

virtual QStringtextFromValue(int value) const
virtual intvalueFromText(const QString & text) const

Reimplemented Protected Functions

virtual boolevent(QEvent * event)
virtual voidfixup(QString & input) const
virtual QValidator::Statevalidate(QString & text, int & pos) const

Additional Inherited Members

  • 4 static public members inherited fromQWidget
  • 7 static public members inherited fromQObject
  • 1 protected slot inherited fromQWidget

Detailed Description

TheQSpinBox class provides a spin box widget.

QSpinBox is designed to handle integers and discrete sets of values (e.g., month names); useQDoubleSpinBox for floating point values.

QSpinBox allows the user to choose a value by clicking the up/down buttons or pressing up/down on the keyboard to increase/decrease the value currently displayed. The user can also type the value in manually. The spin box supports integer values but can be extended to use different strings withvalidate(),textFromValue() andvalueFromText().

Every time the value changesQSpinBox emits thevalueChanged() signals. The current value can be fetched withvalue() and set withsetValue().

Clicking the up/down buttons or using the keyboard accelerator's up and down arrows will increase or decrease the current value in steps of sizesingleStep(). If you want to change this behaviour you can reimplement the virtual functionstepBy(). The minimum and maximum value and the step size can be set using one of the constructors, and can be changed later withsetMinimum(),setMaximum() andsetSingleStep().

Most spin boxes are directional, butQSpinBox can also operate as a circular spin box, i.e. if the range is 0-99 and the current value is 99, clicking "up" will give 0 ifwrapping() is set to true. UsesetWrapping() if you want circular behavior.

The displayed value can be prepended and appended with arbitrary strings indicating, for example, currency or the unit of measurement. SeesetPrefix() andsetSuffix(). The text in the spin box is retrieved withtext() (which includes anyprefix() andsuffix()), or withcleanText() (which has noprefix(), nosuffix() and no leading or trailing whitespace).

It is often desirable to give the user a special (often default) choice in addition to the range of numeric values. SeesetSpecialValueText() for how to do this withQSpinBox.

Screenshot of a Windows XP spin boxA spin box shown in theWindows XP widget style.
Screenshot of a Plastique spin boxA spin box shown in thePlastique widget style.
Screenshot of a Macintosh spin boxA spin box shown in theMacintosh widget style.

Subclassing QSpinBox

If usingprefix(),suffix(), andspecialValueText() don't provide enough control, you subclassQSpinBox and reimplementvalueFromText() andtextFromValue(). For example, here's the code for a custom spin box that allows the user to enter icon sizes (e.g., "32 x 32"):

int IconSizeSpinBox::valueFromText(constQString&text)const{QRegExp regExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?"));if (regExp.exactMatch(text)) {return regExp.cap(1).toInt();    }else {return0;    }}QString IconSizeSpinBox::textFromValue(int value)const{return tr("%1 x %1").arg(value);}

See theIcons example for the full source code.

See alsoQDoubleSpinBox,QDateTimeEdit,QSlider, andSpin Boxes Example.

Property Documentation

cleanText : constQString

This property holds the text of the spin box excluding any prefix, suffix, or leading or trailing whitespace.

Access functions:

QStringcleanText() const

See alsotext,QSpinBox::prefix, andQSpinBox::suffix.

maximum :int

This property holds the maximum value of the spin box.

When setting this property theminimum is adjusted if necessary, to ensure that the range remains valid.

The default maximum value is 99.

Access functions:

intmaximum() const
voidsetMaximum(int max)

See alsosetRange() andspecialValueText.

minimum :int

This property holds the minimum value of the spin box.

When setting this property themaximum is adjusted if necessary to ensure that the range remains valid.

The default minimum value is 0.

Access functions:

intminimum() const
voidsetMinimum(int min)

See alsosetRange() andspecialValueText.

prefix :QString

This property holds the spin box's prefix.

The prefix is prepended to the start of the displayed value. Typical use is to display a unit of measurement or a currency symbol. For example:

sb->setPrefix("$");

To turn off the prefix display, set this property to an empty string. The default is no prefix. The prefix is not displayed whenvalue() ==minimum() andspecialValueText() is set.

If no prefix is set, prefix() returns an empty string.

Access functions:

QStringprefix() const
voidsetPrefix(const QString & prefix)

See alsosuffix(),setSuffix(),specialValueText(), andsetSpecialValueText().

singleStep :int

This property holds the step value.

When the user uses the arrows to change the spin box's value the value will be incremented/decremented by the amount of the singleStep. The default value is 1. Setting a singleStep value of less than 0 does nothing.

Access functions:

intsingleStep() const
voidsetSingleStep(int val)

suffix :QString

This property holds the suffix of the spin box.

The suffix is appended to the end of the displayed value. Typical use is to display a unit of measurement or a currency symbol. For example:

sb->setSuffix(" km");

To turn off the suffix display, set this property to an empty string. The default is no suffix. The suffix is not displayed for theminimum() ifspecialValueText() is set.

If no suffix is set, suffix() returns an empty string.

Access functions:

QStringsuffix() const
voidsetSuffix(const QString & suffix)

See alsoprefix(),setPrefix(),specialValueText(), andsetSpecialValueText().

value :int

This property holds the value of the spin box.

setValue() will emit valueChanged() if the new value is different from the old one.

Access functions:

intvalue() const
voidsetValue(int val)

Notifier signal:

voidvalueChanged(int i)
voidvalueChanged(const QString & text)

Member Function Documentation

QSpinBox::QSpinBox(QWidget * parent = 0)

Constructs a spin box with 0 as minimum value and 99 as maximum value, a step value of 1. The value is initially set to 0. It is parented toparent.

See alsosetMinimum(),setMaximum(), andsetSingleStep().

[virtual protected]bool QSpinBox::event(QEvent * event)

Reimplemented fromQObject::event().

[virtual protected]void QSpinBox::fixup(QString & input) const

Reimplemented fromQAbstractSpinBox::fixup().

void QSpinBox::setRange(int minimum,int maximum)

Convenience function to set theminimum, andmaximum values with a single function call.

setRange(minimum, maximum);

is equivalent to:

setMinimum(minimum);setMaximum(maximum);

See alsominimum andmaximum.

[virtual protected]QString QSpinBox::textFromValue(int value) const

This virtual function is used by the spin box whenever it needs to display the givenvalue. The default implementation returns a string containingvalue printed in the standard way usingQWidget::locale().toString(), but with the thousand separator removed. Reimplementations may return anything. (See the example in the detailed description.)

Note:QSpinBox does not call this function forspecialValueText() and that neitherprefix() norsuffix() should be included in the return value.

If you reimplement this, you may also need to reimplementvalueFromText() andvalidate()

See alsovalueFromText(),validate(), andQLocale::groupSeparator().

[virtual protected]QValidator::State QSpinBox::validate(QString & text,int & pos) const

Reimplemented fromQAbstractSpinBox::validate().

[virtual protected]int QSpinBox::valueFromText(constQString & text) const

This virtual function is used by the spin box whenever it needs to interprettext entered by the user as a value.

Subclasses that need to display spin box values in a non-numeric way need to reimplement this function.

Note:QSpinBox handlesspecialValueText() separately; this function is only concerned with the other values.

See alsotextFromValue() andvalidate().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp