How to restrict QSpinBox to take more the one Zero ??



  • Hi,

    I have this slot in mainwindow.cpp
    void MainWindow::on_blend_valueChanged(int arg1)
    {
    }
    in the UI in the blend field,
    A field can allow upto max only one zero and a non zero number cannot be prefixed with a 0. For example : 0,1,2 is allowed ,but not 00,01,002..

    how can i do this,please help me.


  • Lifetime Qt Champion

    Hi,

    From the top of my head:

    Out of curiosity, why do you want to limit the number of zeroes ?



  • thank you @SGaist
    I am also thinking the same,but its a useless requirement.
    Have to do.



  • Hello,

    I may have misunderstood, but if you want value to be always the same size and prefixed with 0, you can subclass QSpinBox and reimplement textFromValue():

    class CustomSpinBox : public QSpinBox
    {
        Q_OBJECT
    public:
        explicit CustomSpinBox(QWidget *parent = nullptr) : QSpinBox(parent)
        {}
    
    protected:
        virtual QString textFromValue(int val) const
        {
            //Ensure "width" of the field is always 4
            //base 10 (decimal)
            //fill with '0' character
            return QString("%1").arg(val, 4, 10, QChar('0'));
        }
    };
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.