Qt introduction series development tutorial [basic controls] QSpinBox rotating box

Posted by fourteen00 on Fri, 04 Feb 2022 07:54:10 +0100

describe

QSpinBox is designed to handle integer and discrete value sets (for example, month names); Use QDoubleSpinBox for floating-point values.
QSpinBox allows the user to increase / decrease the currently displayed value by clicking the up / down button or pressing the up / down key on the keyboard. You can also enter values manually. The spin box supports integer values, but can be extended to use different strings through validate(), textFromValue(), and valueFromText().
Each time the value changes, QSpinBox will send out valueChanged() and textChanged() signals. The former provides an int and the latter provides a QString. The textChanged() signal provides values with prefix() and suffix(). You can use value() to get the current value and setValue() to set it.
Clicking the up / down button or using the up and down arrows of the keyboard accelerator will increase or decrease the current value in single step (). If you want to change this behavior, you can re implement the virtual function stepBy(). You can use one of the constructors to set the minimum and maximum values and the step size, which can be changed later using setMinimum(), setMaximum(), and setSingleStep().
Most rotation boxes are oriented, but QSpinBox can also be run as a circular rotation box. That is, if the range is 0-99, the current value is 99. If wrapping() is set to true, clicking up will give 0. If you want to loop behavior, use setWrapping().
The displayed value can be added to any string, such as currency or unit of measure. See setPrefix() and setSuffix(). The text in the spin box is retrieved using text() (including any prefix() and suffix()) or cleanText() (without prefix(), suffix(), and no leading or trailing spaces).
In addition to the numerical range, you usually need to give the user a special (usually default) choice. See setSpecialValueText() for how to do this using QSpinBox.

If using prefix(), suffix(), and specialValueText() does not provide enough control, you can inherit QSpinBox and re implement valueFromText() and textFromValue(). For example, this is the code for a custom rotation box, which allows the user to enter the icon size (for example, "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);
 }

API

//Clear text
QString cleanText() const
//This property holds the cardinality used to display the rotation box value
 int displayIntegerBase() const
//Get maximum
 int maximum() const
 //Get minimum value
 int minimum() const
 //Get prefix
 QString prefix() const
 //Set hexadecimal number
 void setDisplayIntegerBase(int base)
 //Set maximum value
 void setMaximum(int max)
 //Set minimum value
 void setMinimum(int min)
 //Set suffix
 void setPrefix(const QString &prefix)
 //set range
 void setRange(int minimum, int maximum)
 //Set step size
 void setSingleStep(int val)
//Set single step or adaptive decimal step.
 void setStepType(QAbstractSpinBox::StepType stepType)
 //Set prefix
 void setSuffix(const QString &suffix)
 //Get step size
 int singleStep() const
 //Get step type
 QAbstractSpinBox::StepType stepType() const
//Get prefix
 QString suffix() const
 //Get value
 int value() const

Topics: Qt