QAbstractSpinBox Class

QAbstractSpinBox 提供一个选择框和一个行编辑显示值. 更多...

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

QDateTimeEdit, QDoubleSpinBox, and QSpinBox

公有类型

enum ButtonSymbols { UpDownArrows, PlusMinus, NoButtons }
enum CorrectionMode { CorrectToPreviousValue, CorrectToNearestValue }
flags StepEnabled
enum StepEnabledFlag { StepNone, StepUpEnabled, StepDownEnabled }

属性

公有函数

QAbstractSpinBox(QWidget *parent = Q_NULLPTR)
~QAbstractSpinBox()
Qt::Alignment alignment() const
ButtonSymbols buttonSymbols() const
CorrectionMode correctionMode() const
virtual void fixup(QString &input) const
bool hasAcceptableInput() const
bool hasFrame() const
void interpretText()
bool isAccelerated() const
bool isGroupSeparatorShown() const
bool isReadOnly() const
bool keyboardTracking() const
void setAccelerated(bool on)
void setAlignment(Qt::Alignment flag)
void setButtonSymbols(ButtonSymbols bs)
void setCorrectionMode(CorrectionMode cm)
void setFrame(bool)
void setGroupSeparatorShown(bool shown)
void setKeyboardTracking(bool kt)
void setReadOnly(bool r)
void setSpecialValueText(const QString &txt)
void setWrapping(bool w)
QString specialValueText() const
virtual void stepBy(int steps)
QString text() const
virtual QValidator::State validate(QString &input, int &pos) const
bool wrapping() const

重新实现的公有函数

virtual bool event(QEvent *event) override
virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override
virtual QSize minimumSizeHint() const override
virtual QSize sizeHint() const override

公有槽函数

virtual void clear()
void selectAll()
void stepDown()
void stepUp()
  • 19 个公有槽函数继承自 QWidget
  • 1 个公有槽函数继承自 QObject

信号

受保护的函数

void initStyleOption(QStyleOptionSpinBox *option) const
QLineEdit *lineEdit() const
void setLineEdit(QLineEdit *lineEdit)
virtual StepEnabled stepEnabled() const

重新实现的受保护函数

virtual void changeEvent(QEvent *event) override
virtual void closeEvent(QCloseEvent *event) override
virtual void contextMenuEvent(QContextMenuEvent *event) override
virtual void focusInEvent(QFocusEvent *event) override
virtual void focusOutEvent(QFocusEvent *event) override
virtual void hideEvent(QHideEvent *event) override
virtual void keyPressEvent(QKeyEvent *event) override
virtual void keyReleaseEvent(QKeyEvent *event) override
virtual void mouseMoveEvent(QMouseEvent *event) override
virtual void mousePressEvent(QMouseEvent *event) override
virtual void mouseReleaseEvent(QMouseEvent *event) override
virtual void paintEvent(QPaintEvent *event) override
virtual void resizeEvent(QResizeEvent *event) override
virtual void showEvent(QShowEvent *event) override
virtual void timerEvent(QTimerEvent *event) override
virtual void wheelEvent(QWheelEvent *event) override
  • 35 个受保护的函数继承自 QWidget
  • 9 个受保护的函数继承自 QObject
  • 1 个受保护的函数继承自 QPaintDevice

其他继承的成员

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

详细描述

QAbstractSpinBox 提供一个选择框和一个行编辑显示值.

这个类 QSpinBox, QDoubleSpinBoxQDateTimeEdit的超类

下面是类的主要属性:

  1. text: 显示文本.
  2. alignment: 文本对齐方式.
  3. wrapping: 值的范围[minimum, maximum].

QAbstractSpinBox 提供虚函数 stepBy() , 用户每一步操作都会调用该函数. 此函数的整型参数表示执行多少步. 即. 按下 Qt::Key_Down, 调用 stepBy(-1).

QAbstractSpinBox 还提供了一个虚拟函数 stepEnabled(), 检测是否允许任何点上执行递增/递减操作. 此函数返回StepEnabled的位集.

参见 QAbstractSlider, QSpinBox, QDoubleSpinBox, QDateTimeEdit, and Spin Boxes Example.

成员类型

enum QAbstractSpinBox::ButtonSymbols

This enum type describes the symbols that can be displayed on the buttons in a spin box.

ConstantValueDescription
QAbstractSpinBox::UpDownArrows0Little arrows in the classic style.
QAbstractSpinBox::PlusMinus1+ and - symbols.
QAbstractSpinBox::NoButtons2Don't display buttons.

参见 QAbstractSpinBox::buttonSymbols.

enum QAbstractSpinBox::CorrectionMode

This enum type describes the mode the spinbox will use to correct an Intermediate value if editing finishes.

ConstantValueDescription
QAbstractSpinBox::CorrectToPreviousValue0The spinbox will revert to the last valid value.
QAbstractSpinBox::CorrectToNearestValue1The spinbox will revert to the nearest valid value.

参见 correctionMode.

enum QAbstractSpinBox::StepEnabledFlag
flags QAbstractSpinBox::StepEnabled

ConstantValue
QAbstractSpinBox::StepNone0x00
QAbstractSpinBox::StepUpEnabled0x01
QAbstractSpinBox::StepDownEnabled0x02

The StepEnabled type is a typedef for QFlags<StepEnabledFlag>. It stores an OR combination of StepEnabledFlag values.

属性

accelerated : bool

This property holds whether the spin box will accelerate the frequency of the steps when pressing the step Up/Down buttons.

If enabled the spin box will increase/decrease the value faster the longer you hold the button down.

This property was introduced in Qt 4.2.

访问函数:

bool isAccelerated() const
void setAccelerated(bool on)

acceptableInput : const bool

This property holds whether the input satisfies the current validation

This property was introduced in Qt 4.2.

访问函数:

bool hasAcceptableInput() const

参见 validate(), fixup(), and correctionMode.

alignment : Qt::Alignment

This property holds the alignment of the spin box

Possible Values are Qt::AlignLeft, Qt::AlignRight, and Qt::AlignHCenter.

By default, the alignment is Qt::AlignLeft

Attempting to set the alignment to an illegal flag combination does nothing.

访问函数:

Qt::Alignment alignment() const
void setAlignment(Qt::Alignment flag)

参见 Qt::Alignment.

buttonSymbols : ButtonSymbols

This property holds the current button symbol mode

The possible values can be either UpDownArrows or PlusMinus. The default is UpDownArrows.

Note that some styles might render PlusMinus and UpDownArrows identically.

访问函数:

ButtonSymbols buttonSymbols() const
void setButtonSymbols(ButtonSymbols bs)

参见 ButtonSymbols.

correctionMode : CorrectionMode

This property holds the mode to correct an Intermediate value if editing finishes

The default mode is QAbstractSpinBox::CorrectToPreviousValue.

This property was introduced in Qt 4.2.

访问函数:

CorrectionMode correctionMode() const
void setCorrectionMode(CorrectionMode cm)

参见 acceptableInput, validate(), and fixup().

frame : bool

This property holds whether the spin box draws itself with a frame

If enabled (the default) the spin box draws itself inside a frame, otherwise the spin box draws itself without any frame.

访问函数:

bool hasFrame() const
void setFrame(bool)

keyboardTracking : bool

This property holds whether keyboard tracking is enabled for the spinbox.

If keyboard tracking is enabled (the default), the spinbox emits the valueChanged() signal while the new value is being entered from the keyboard.

E.g. when the user enters the value 600 by typing 6, 0, and 0, the spinbox emits 3 signals with the values 6, 60, and 600 respectively.

If keyboard tracking is disabled, the spinbox doesn't emit the valueChanged() signal while typing. It emits the signal later, when the return key is pressed, when keyboard focus is lost, or when other spinbox functionality is used, e.g. pressing an arrow key.

This property was introduced in Qt 4.3.

访问函数:

bool keyboardTracking() const
void setKeyboardTracking(bool kt)

readOnly : bool

This property holds whether the spin box is read only.

In read-only mode, the user can still copy the text to the clipboard, or drag and drop the text; but cannot edit it.

The QLineEdit in the QAbstractSpinBox does not show a cursor in read-only mode.

访问函数:

bool isReadOnly() const
void setReadOnly(bool r)

参见 QLineEdit::readOnly.

showGroupSeparator : bool

This property holds whether a thousands separator is enabled. By default this property is false.

This property was introduced in Qt 5.3.

访问函数:

bool isGroupSeparatorShown() const
void setGroupSeparatorShown(bool shown)

specialValueText : QString

This property holds the special-value text

If set, the spin box will display this text instead of a numeric value whenever the current value is equal to minimum(). Typical use is to indicate that this choice has a special (default) meaning.

For example, if your spin box allows the user to choose a scale factor (or zoom level) for displaying an image, and your application is able to automatically choose one that will enable the image to fit completely within the display window, you can set up the spin box like this:


      QSpinBox *zoomSpinBox = new QSpinBox;
      zoomSpinBox->setRange(0, 1000);
      zoomSpinBox->setSingleStep(10);
      zoomSpinBox->setSuffix("%");
      zoomSpinBox->setSpecialValueText(tr("Automatic"));
      zoomSpinBox->setValue(100);

The user will then be able to choose a scale from 1% to 1000% or select "Auto" to leave it up to the application to choose. Your code must then interpret the spin box value of 0 as a request from the user to scale the image to fit inside the window.

All values are displayed with the prefix and suffix (if set), except for the special value, which only shows the special value text. This special text is passed in the QSpinBox::valueChanged() signal that passes a QString.

To turn off the special-value text display, call this function with an empty string. The default is no special-value text, i.e. the numeric value is shown as usual.

If no special-value text is set, specialValueText() returns an empty string.

访问函数:

QString specialValueText() const
void setSpecialValueText(const QString &txt)

text : const QString

This property holds the spin box's text, including any prefix and suffix

There is no default text.

访问函数:

QString text() const

wrapping : bool

This property holds whether the spin box is circular.

If wrapping is true stepping up from maximum() value will take you to the minimum() value and vica versa. Wrapping only make sense if you have minimum() and maximum() values set.


  QSpinBox *spinBox = new QSpinBox(this);
  spinBox->setRange(0, 100);
  spinBox->setWrapping(true);
  spinBox->setValue(100);
  spinBox->stepBy(1);
  // value is 0

访问函数:

bool wrapping() const
void setWrapping(bool w)

参见 QSpinBox::minimum() and QSpinBox::maximum().

成员函数

QAbstractSpinBox::QAbstractSpinBox(QWidget *parent = Q_NULLPTR)

Constructs an abstract spinbox with the given parent with default wrapping, and alignment properties.

QAbstractSpinBox::~QAbstractSpinBox()

Called when the QAbstractSpinBox is destroyed.

[override virtual protected] void QAbstractSpinBox::changeEvent(QEvent *event)

重新实现 QWidget::changeEvent().

[virtual slot] void QAbstractSpinBox::clear()

Clears the lineedit of all text but prefix and suffix.

[override virtual protected] void QAbstractSpinBox::closeEvent(QCloseEvent *event)

重新实现 QWidget::closeEvent().

[override virtual protected] void QAbstractSpinBox::contextMenuEvent(QContextMenuEvent *event)

重新实现 QWidget::contextMenuEvent().

[signal] void QAbstractSpinBox::editingFinished()

This signal is emitted editing is finished. This happens when the spinbox loses focus and when enter is pressed.

[override virtual] bool QAbstractSpinBox::event(QEvent *event)

重新实现 QObject::event().

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

This virtual function is called by the QAbstractSpinBox if the input is not validated to QValidator::Acceptable when Return is pressed or interpretText() is called. It will try to change the text so it is valid. Reimplemented in the various subclasses.

[override virtual protected] void QAbstractSpinBox::focusInEvent(QFocusEvent *event)

重新实现 QWidget::focusInEvent().

[override virtual protected] void QAbstractSpinBox::focusOutEvent(QFocusEvent *event)

重新实现 QWidget::focusOutEvent().

[override virtual protected] void QAbstractSpinBox::hideEvent(QHideEvent *event)

重新实现 QWidget::hideEvent().

[protected] void QAbstractSpinBox::initStyleOption(QStyleOptionSpinBox *option) const

Initialize option with the values from this QSpinBox. This method is useful for subclasses when they need a QStyleOptionSpinBox, but don't want to fill in all the information themselves.

参见 QStyleOption::initFrom().

[override virtual] QVariant QAbstractSpinBox::inputMethodQuery(Qt::InputMethodQuery query) const

重新实现 QWidget::inputMethodQuery().

void QAbstractSpinBox::interpretText()

This function interprets the text of the spin box. If the value has changed since last interpretation it will emit signals.

[override virtual protected] void QAbstractSpinBox::keyPressEvent(QKeyEvent *event)

重新实现 QWidget::keyPressEvent().

This function handles keyboard input.

The following keys are handled specifically:

Enter/ReturnThis will reinterpret the text and emit a signal even if the value has not changed since last time a signal was emitted.
UpThis will invoke stepBy(1)
DownThis will invoke stepBy(-1)
Page upThis will invoke stepBy(10)
Page downThis will invoke stepBy(-10)

[override virtual protected] void QAbstractSpinBox::keyReleaseEvent(QKeyEvent *event)

重新实现 QWidget::keyReleaseEvent().

[protected] QLineEdit *QAbstractSpinBox::lineEdit() const

This function returns a pointer to the line edit of the spin box.

参见 setLineEdit().

[override virtual] QSize QAbstractSpinBox::minimumSizeHint() const

重新实现 QWidget::minimumSizeHint().

[override virtual protected] void QAbstractSpinBox::mouseMoveEvent(QMouseEvent *event)

重新实现 QWidget::mouseMoveEvent().

[override virtual protected] void QAbstractSpinBox::mousePressEvent(QMouseEvent *event)

重新实现 QWidget::mousePressEvent().

[override virtual protected] void QAbstractSpinBox::mouseReleaseEvent(QMouseEvent *event)

重新实现 QWidget::mouseReleaseEvent().

[override virtual protected] void QAbstractSpinBox::paintEvent(QPaintEvent *event)

重新实现 QWidget::paintEvent().

[override virtual protected] void QAbstractSpinBox::resizeEvent(QResizeEvent *event)

重新实现 QWidget::resizeEvent().

[slot] void QAbstractSpinBox::selectAll()

Selects all the text in the spinbox except the prefix and suffix.

[protected] void QAbstractSpinBox::setLineEdit(QLineEdit *lineEdit)

Sets the line edit of the spinbox to be lineEdit instead of the current line edit widget. lineEdit can not be 0.

QAbstractSpinBox takes ownership of the new lineEdit

If QLineEdit::validator() for the lineEdit returns 0, the internal validator of the spinbox will be set on the line edit.

参见 lineEdit().

[override virtual protected] void QAbstractSpinBox::showEvent(QShowEvent *event)

重新实现 QWidget::showEvent().

[override virtual] QSize QAbstractSpinBox::sizeHint() const

重新实现 QWidget::sizeHint().

[virtual] void QAbstractSpinBox::stepBy(int steps)

Virtual function that is called whenever the user triggers a step. The steps parameter indicates how many steps were taken, e.g. Pressing Qt::Key_Down will trigger a call to stepBy(-1), whereas pressing Qt::Key_Prior will trigger a call to stepBy(10).

If you subclass QAbstractSpinBox you must reimplement this function. Note that this function is called even if the resulting value will be outside the bounds of minimum and maximum. It's this function's job to handle these situations.

[slot] void QAbstractSpinBox::stepDown()

Steps down by one linestep Calling this slot is analogous to calling stepBy(-1);

参见 stepBy() and stepUp().

[virtual protected] StepEnabled QAbstractSpinBox::stepEnabled() const

Virtual function that determines whether stepping up and down is legal at any given time.

The up arrow will be painted as disabled unless (stepEnabled() & StepUpEnabled) != 0.

The default implementation will return (StepUpEnabled| StepDownEnabled) if wrapping is turned on. Else it will return StepDownEnabled if value is > minimum() or'ed with StepUpEnabled if value < maximum().

If you subclass QAbstractSpinBox you will need to reimplement this function.

参见 QSpinBox::minimum(), QSpinBox::maximum(), and wrapping().

[slot] void QAbstractSpinBox::stepUp()

Steps up by one linestep Calling this slot is analogous to calling stepBy(1);

参见 stepBy() and stepDown().

[override virtual protected] void QAbstractSpinBox::timerEvent(QTimerEvent *event)

重新实现 QObject::timerEvent().

[virtual] QValidator::State QAbstractSpinBox::validate(QString &input, int &pos) const

This virtual function is called by the QAbstractSpinBox to determine whether input is valid. The pos parameter indicates the position in the string. Reimplemented in the various subclasses.

[override virtual protected] void QAbstractSpinBox::wheelEvent(QWheelEvent *event)

重新实现 QWidget::wheelEvent().