QDoubleSpinBox : Setting thousand separator while user is typing cursor position bug



  • Hi!

    I want to make a QDoubleSpinBox that would format the value into currency so it would be readable by users.

    So far i've accomplished these things:

    • set showGroupSeparator to true - but i will only work when focus is released.
    • used the valueChange to update the amount by ui->doubleSpinBox->setValue(amount);

    The cursor position will not be in the correct position when the amount is > than 10k, sometimes the amount will completely disappear. in short its really buggy.

    is there any approach on this? Thanks!



  • @binsoii said in QDoubleSpinBox : Setting thousand separator while user is typing cursor position bug:

    in short its really buggy.

    Well... what you describe above is the exact same behaviour you obtain in MS Office so we might as well argue this behaves as intended and there are no bugs.

    You might probably be better off building your own custom widget with QLineEdit+QValidator+2*QPushButton



  • @VRonin Thanks! gosh building my own custom widget is quite overwhelming. can you give some pointers or tips about this? :)



  • @binsoii said in QDoubleSpinBox : Setting thousand separator while user is typing cursor position bug:

    gosh building my own custom widget is quite overwhelming

    The below is just a starting point

    #include <QWidget>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QDoubleValidator>
    #include <QGridLayout>
    class TestUser : public QWidget
    {
        Q_OBJECT
        Q_DISABLE_COPY(TestUser)
    public:
        explicit TestUser(QWidget *parent = Q_NULLPTR)
            :QWidget(parent)
            , m_step(1.0)
        {
            m_editor=new QLineEdit(this);
            m_editor->setValidator(new QDoubleValidator(this));
            m_editor->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
            m_increaseButton=new QPushButton(QStringLiteral("\u23F6"),this);
            m_decreaseButton=new QPushButton(QStringLiteral("\u23F7"),this);
            connect(m_increaseButton,&QPushButton::clicked,this,[this](){m_editor->setText(m_editor->locale().toString(m_editor->locale().toDouble(m_editor->text())+m_step));});
            connect(m_decreaseButton,&QPushButton::clicked,this,[this](){m_editor->setText(m_editor->locale().toString(m_editor->locale().toDouble(m_editor->text())-m_step));});
            connect(m_editor,&QLineEdit::textChanged,this,[this](){Q_EMIT valueChanged(m_editor->locale().toDouble(m_editor->text()));});
            QGridLayout* mainLay=new QGridLayout(this);
            mainLay->setSpacing(0);
            mainLay->addWidget(m_editor,0,0,2,1);
            mainLay->addWidget(m_increaseButton,0,1);
            mainLay->addWidget(m_decreaseButton,1,1);
        }
        double step() const {return m_step;}
        void setStep(double step){if(m_step!=step){m_step=step; Q_EMIT stepChanged(m_step);}}
        Q_SIGNAL void stepChanged(double);
        Q_SIGNAL void valueChanged(double);
    private:
        QLineEdit* m_editor;
        QPushButton* m_increaseButton;
        QPushButton* m_decreaseButton;
        double m_step;
    };
    

Log in to reply
 

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