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?

+3


source to share


3 answers


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

+9


source


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 .

+5


source


Subclass QSpinBox and define your behavior.
Take a look at an example

-1


source







All Articles