QSpinBox Class

QSpinBox 提供数字调整框. 更多...

头文件: #include <QSpinBox>
qmake: QT += widgets
基类: QAbstractSpinBox

属性

公有函数

QSpinBox(QWidget *parent = Q_NULLPTR)
~QSpinBox()
QString cleanText() const
int displayIntegerBase() const
int maximum() const
int minimum() const
QString prefix() const
void setDisplayIntegerBase(int base)
void setMaximum(int max)
void setMinimum(int min)
void setPrefix(const QString &prefix)
void setRange(int minimum, int maximum)
void setSingleStep(int val)
void setSuffix(const QString &suffix)
int singleStep() const
QString suffix() const
int value() const

公有槽函数

void setValue(int val)

信号

void valueChanged(int i)
void valueChanged(const QString &text)

受保护的函数

virtual QString textFromValue(int value) const
virtual int valueFromText(const QString &text) const

重新实现的受保护函数

virtual bool event(QEvent *event) override
virtual void fixup(QString &input) const override
virtual QValidator::State validate(QString &text, int &pos) const override

其他继承的成员

  • 1 个公有变量继承自 QObject
  • 5 个静态公有成员继承自 QWidget
  • 10 个静态公有成员继承自 QObject
  • 1 个受保护的槽函数继承自 QWidget
  • 2 个受保护的变量继承自 QObject
  • 1 protected type inherited from QPaintDevice

详细描述

QSpinBox 提供数字调整框.

QSpinBox 处理数字和离散数 (如., 月); 使用 QDoubleSpinBox 处理浮点数.

QSpinBox 允许用户点击界面的上下箭头(或键盘的上下方向键)修改显示值. 同时, QSpinBox也允许手动修改. 数字调整框支持双精度浮点数, 还可以使用 validate(), textFromValue() 和 valueFromText()支持字符串.

值每次修改, QSpinBox 都会发送 valueChanged() 信号, 这个信号函数有两个重载函数, 支持浮点型和 QString. QString 重载函数的字符串参数带有 prefix() 和 suffix(). QSpinBox值调用 value() 获取, 调用 setValue() 设置.

点击界面上下箭头或键盘上下方向键修改值, 步长是 singleStep(). 如果你想自定义步长, 可以重新实现虚函数 stepBy(). 最小值,最大值及步长可以通过构造函数设置, 创建对象后可调用 setMinimum(), setMaximum() 及 setSingleStep()设置.

多数数字调整框是定向的, 但是 QSpinBox 是一个环形调整框, 即. 如果当前范围是0-99, 当前数字是99, 并且wrapping()是true, 那么单击向上箭头, 数字将变成0. 调用 setWrapping() 设置环形模式.

QSpinBox可以显示表示货币或度量单位的任意字符串. 参见 setPrefix() 和 setSuffix(). 调整框中的文本调用 text()获取 (文本包含 prefix() 和 suffix()), 或调用 cleanText() 获取(文本不包含 prefix(), 也不包含 suffix() 及首尾空格).

除数字范围外, QSpinBox还提供一个特殊选择(通常是默认的). 详见 setSpecialValueText().

Screenshot of a Windows Vista spin boxA spin box shown in the Windows Vista widget style.
Screenshot of a Fusion spin boxA spin box shown in the Fusion widget style.
Screenshot of a Macintosh spin boxA spin box shown in the Macintosh widget style.

子类化 QSpinBox

如果 prefix(), suffix(), specialValueText() 无法满足控制需求, 你可以子类化 QSpinBox, 并重新实现 valueFromText() 和 textFromValue(). 例如, 下面自定义数字调整框, 允许用户改变图标大小 (例如., "32 x 32"):


  int IconSizeSpinBox::valueFromText(const QString &text) const
  {
      static const QRegularExpression regExp(tr("(\\d+)(\\s*[xx]\\s*\\d+)?"));
      Q_ASSERT(regExp.isValid());

      const QRegularExpressionMatch match = regExp.match(text);
      if (match.isValid())
          return match.captured(1).toInt();
      return 0;
  }

  QString IconSizeSpinBox::textFromValue(int value) const
  {
      return tr("%1 x %1").arg(value);
  }

详见 Icons 示例.

参见 QDoubleSpinBox, QDateTimeEdit, QSlider, and Spin Boxes Example.

属性

cleanText : const QString

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

访问函数:

QString cleanText() const

参见 text, QSpinBox::prefix, and QSpinBox::suffix.

displayIntegerBase : int

This property holds the base used to display the value of the spin box

The default displayIntegerBase value is 10.

This property was introduced in Qt 5.2.

访问函数:

int displayIntegerBase() const
void setDisplayIntegerBase(int base)

参见 textFromValue() and valueFromText().

maximum : int

This property holds the maximum value of the spin box

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

The default maximum value is 99.

访问函数:

int maximum() const
void setMaximum(int max)

参见 setRange() and specialValueText.

minimum : int

This property holds the minimum value of the spin box

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

The default minimum value is 0.

访问函数:

int minimum() const
void setMinimum(int min)

参见 setRange() and specialValueText.

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 when value() == minimum() and specialValueText() is set.

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

访问函数:

QString prefix() const
void setPrefix(const QString &prefix)

参见 suffix(), setSuffix(), specialValueText(), and setSpecialValueText().

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.

访问函数:

int singleStep() const
void setSingleStep(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 the minimum() if specialValueText() is set.

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

访问函数:

QString suffix() const
void setSuffix(const QString &suffix)

参见 prefix(), setPrefix(), specialValueText(), and setSpecialValueText().

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. The value property has a second notifier signal which includes the spin box's prefix and suffix.

访问函数:

int value() const
void setValue(int val)

Notifier signal:

void valueChanged(int i)
void valueChanged(const QString &text)

成员函数

QSpinBox::QSpinBox(QWidget *parent = Q_NULLPTR)

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 to parent.

参见 setMinimum(), setMaximum(), and setSingleStep().

QSpinBox::~QSpinBox()

Destructor.

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

重新实现 QObject::event().

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

重新实现 QAbstractSpinBox::fixup().

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

Convenience function to set the minimum, and maximum values with a single function call.


  setRange(minimum, maximum);

is equivalent to:


  setMinimum(minimum);
  setMaximum(maximum);

参见 minimum and maximum.

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

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

Note: QSpinBox does not call this function for specialValueText() and that neither prefix() nor suffix() should be included in the return value.

If you reimplement this, you may also need to reimplement valueFromText() and validate()

参见 valueFromText(), validate(), and QLocale::groupSeparator().

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

重新实现 QAbstractSpinBox::validate().

[signal] void QSpinBox::valueChanged(int i)

This signal is emitted whenever the spin box's value is changed. The new value's integer value is passed in i.

Note: Signal valueChanged is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:


  connect(spinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged),
      [=](int i){ /* ... */ });

Note: Notifier signal for property value.

[signal] void QSpinBox::valueChanged(const QString &text)

This is an overloaded function.

The new value is passed in text with prefix() and suffix().

Note: Signal valueChanged is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:


  connect(spinBox, static_cast<void(QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged),
      [=](const QString &text){ /* ... */ });

Note: Notifier signal for property value.

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

This virtual function is used by the spin box whenever it needs to interpret text 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 handles specialValueText() separately; this function is only concerned with the other values.

参见 textFromValue() and validate().