Important: Please read the Qt Code of Conduct -

QDoubleSpinBox without annoying zeros after decimal point

  • Hey!
    I was wondering, if there is a simple way to make QDoubleSpibBox not to show all 10 zeros, If i set setDecimal(10)? I have spent several hours today, trying to figure out how to do that. My first idea was to add setCorrectDecimal(double d) slot, and connect it to valueChanged(double d) signal of QDoubleSpinBox. setCorrectDecimal set decimal on fly. It works! But the problem is that it work only when I use arrows to change value. If I try to change value by entering number from keybord, it doesn't work. I also have tried to override validate() method of QDoubleSpinBox in my own DoubleSpinBox class, but it doesn't work as I want.
    Any Ideas?

  • Perhaps by checking the value before it is shown, maybe in valueChanged?
    And then setting the decimal after checking which decimal != 0?

    Another place for something like this could be the paintEvent.

  • Moderators

  • @kshegunov It work!
    But if the value of QDoubleSpinBox is 1 (double 1.0), after calling of setDecimals(10) you will see ten zeros after decimal point (1.0000000000), which looks extremely ugly. Especially when I want my QDoubleSpinBoxes accept values from 10e-15 to 10e15.

  • Moderators

    Why do you want the double spin box to accept such values?! What you're requesting is 30 orders of magnitude - roughly how the gold atom's nucleus' radius (about 7 fm) relates to the distance between Neptune and the Sun (about 4.5 billion kilometers) ... and then some more ...

  • @kshegunov Hey! :)
    I know, if you are interested what i am doing, I will explain. Yes, maybe 10e15 is really too much. But still i need really wide span. I am doing software which reads different signals from files, plot them, manage and do basic analysis. So, it is some kind of oscilloscope, but it is more about visual representation and data managament, than analysis. So values of signals might be really different, it could be giga watts or it could be nano meters. So, after all, I think I really need this span. And this problem really is driving me crazy, right now I am sitting and reading Qt source code, to understand how does QDoubleSpinBox works internally.

  • Moderators

    This just isn't practical. What would be the step for such a spin box?
    Just have a spinner that is between 999 and 0.001 and put a complementary dropdown with the measurement unit.

  • @kshegunov Well, I still think that it is practical. User shouldn't be imposed by such kind of restrictions. It always freak out anyone. In any case, thanks for your reply, course you gave me an idea to use just QLineText and QDoubleValidator instead of QDoubleSpinBox. Now it works perfectly, any double number can be entered from the keyboard. This is exectely what I wanted.

Log in to reply