Set string to QDoubleSpinBox
As for some task, I have to do the following. I have a QDoubleSpinBox with positive values between 0 and 1000. And every time the user tries to lower the spinbox value, i.e. to press the down button when the value is 0, the spinbox value must be "unset". I tried to do this to clear the spinbox value and then setPrefix. But it didn't work. Any other solutions?
source to share
You can try specialValueText
.
This is an example:
#include <QApplication>
#include <QDoubleSpinBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDoubleSpinBox doubleSpinBox;
doubleSpinBox.setRange(0.f, 1000.f);
doubleSpinBox.setValue(1.f);
doubleSpinBox.setSpecialValueText("unset");
doubleSpinBox.show();
return a.exec();
}
when the value is 0, the spinbox value must be the string "unset".
source to share
There are two solutions to achieve what you want.
As said in another answer, using setSpecialValueText where text is displayed when current value () == minimum () with counter
or slightly more powerful: subclass QSpinBox and override the following functions:
int valueFromText(const QString &text) const;
QString textFromValue(int value) const;
QValidator::State validate(QString & input, int & pos) const;
Have a look here, try something like QSpinBox enter NaN as a valid value .
source to share