QDoubleSpinBox Class

QDoubleSpinBox 提供一个双精度浮点数调整框. 更多...

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

属性

公有函数

QDoubleSpinBox(QWidget *parent = Q_NULLPTR)
~QDoubleSpinBox()
QString cleanText() const
int decimals() const
double maximum() const
double minimum() const
QString prefix() const
void setDecimals(int prec)
void setMaximum(double max)
void setMinimum(double min)
void setPrefix(const QString &prefix)
void setRange(double minimum, double maximum)
void setSingleStep(double val)
void setSuffix(const QString &suffix)
double singleStep() const
QString suffix() const
virtual QString textFromValue(double value) const
double value() const
virtual double valueFromText(const QString &text) const

重新实现的公有函数

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

公有槽函数

void setValue(double val)

信号

void valueChanged(double d)
void valueChanged(const QString &text)

其他继承的成员

详细描述

QDoubleSpinBox 提供一个双精度浮点数调整框.

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

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

注意: QDoubleSpinBox 根据设置精度对浮点数四舍五入. 例如: QDoubleSpinBox 的设置精度是2, 调用 setValue(2.555)后, 再调用value() 将返回 2.56.

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

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

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

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

注意: 除前缀和后缀外, QDoubleSpinBox 的显示值限制为 18 个字符. 此限制也适用显示数字.

参见 QSpinBox, QDateTimeEdit, QSlider, 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, QDoubleSpinBox::prefix, and QDoubleSpinBox::suffix.

decimals : int

This property holds the precision of the spin box, in decimals

Sets how many decimals the spinbox will use for displaying and interpreting doubles.

Warning: The maximum value for decimals is DBL_MAX_10_EXP + DBL_DIG (ie. 323) because of the limitations of the double type.

Note: The maximum, minimum and value might change as a result of changing this property.

访问函数:

int decimals() const
void setDecimals(int prec)

maximum : double

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.99.

Note: The maximum value will be rounded to match the decimals property.

访问函数:

double maximum() const
void setMaximum(double max)

参见 decimals and setRange().

minimum : double

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.0.

Note: The minimum value will be rounded to match the decimals property.

访问函数:

double minimum() const
void setMinimum(double min)

参见 decimals, 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:


  spinbox->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 : double

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.0. Setting a singleStep value of less than 0 does nothing.

访问函数:

double singleStep() const
void setSingleStep(double 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:


  spinbox->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 : double

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.

Note: The value will be rounded so it can be displayed with the current setting of decimals.

访问函数:

double value() const
void setValue(double val)

Notifier signal:

void valueChanged(double d)
void valueChanged(const QString &text)

参见 decimals.

成员函数

QDoubleSpinBox::QDoubleSpinBox(QWidget *parent = Q_NULLPTR)

Constructs a spin box with 0.0 as minimum value and 99.99 as maximum value, a step value of 1.0 and a precision of 2 decimal places. The value is initially set to 0.00. The spin box has the given parent.

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

QDoubleSpinBox::~QDoubleSpinBox()

Destructor.

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

重新实现 QAbstractSpinBox::fixup().

void QDoubleSpinBox::setRange(double minimum, double maximum)

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

Note: The maximum and minimum values will be rounded to match the decimals property.


  setRange(minimum, maximum);

is equivalent to:


  setMinimum(minimum);
  setMaximum(maximum);

参见 minimum and maximum.

[virtual] QString QDoubleSpinBox::textFromValue(double 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 using QWidget::locale().toString(value, QLatin1Char('f'), decimals()) and will remove the thousand separator unless setGroupSeparatorShown() is set. Reimplementations may return anything.

Note: QDoubleSpinBox 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().

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

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

重新实现 QAbstractSpinBox::validate().

[signal] void QDoubleSpinBox::valueChanged(double d)

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

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(doubleSpinBox, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
      [=](double d){ /* ... */ });

Note: Notifier signal for property value.

[signal] void QDoubleSpinBox::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(doubleSpinBox, static_cast<void(QDoubleSpinBox::*)(const QString &)>(&QDoubleSpinBox::valueChanged),
      [=](const QString &text){ /* ... */ });

Note: Notifier signal for property value.

[virtual] double QDoubleSpinBox::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: QDoubleSpinBox handles specialValueText() separately; this function is only concerned with the other values.

参见 textFromValue() and validate().