Qt World Summit: Submit your Presentation

about QSpinBox, how to make it emit editingFinished signal when click the "up/down" arrow button ??

  • about QSpinBox, how to make it emit editingFinished signal when click the "up/down" arrow button?
    i do want to emit editingFinished signal when edit text, but when i use editingFinished when click the "up/down" arrow button the effect is not what i want...
    i do want this effect:

    1. emit editingFinished signal when edit text.
    2. emit valueChanged signal when click the "up/down" arrow button.

  • @opengpu2
    You can refer this to emit valueChanged signal.
    editingFinished() happens when the spinbox loses focus and when enter is pressed.

  • @Ratzz i know, but i want the effect like this: when i use the single & slot, the signal is emitted when :

    1. when editting, it emit when editingFinished;
    2. when click the "up/down" arrow button, it emit when valueChanged;

  • anyone help?

  • Lifetime Qt Champion

    You can connect valueChanged() signal with editingFinished() signal, that's all.

  • Qt Champions 2017

    As told by Jsulm as well and you can do the following.


  • Moderators

    use this to determine if the user pressed the up or down buttons:

    void MySpinBox::mousePressEvent(QMouseEvent* event)
        QStyleOptionSpinBox opt;
        if( this->style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxUp).contains(event->pos()) )
            // UP BUTTON PRESSED
        else if( this->style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxDown).contains(event->pos()) )

  • @dheerendra this is not working, on entering any value valuechanged is emitted
    Suppose I want to enter 10000, so valueChanged is emitted with 1, 10, 100, 1000, 10000
    Which I don't want, rather it should emit when user pressed enter after entering 10000.
    So this is done by avoiding valueChanged and connecting to &QAbstractSpinbox::editingFinished.
    But I want the up/down arrows to work also. so for this again valueChanged is required.

    Any suggestion is appreciated.

  • Hi @sayan275,

    Is it that you want the same behavior on editingFinished() & valueChanged() ?

  • Lifetime Qt Champion

    @sayan275 In my opinion it does not make sense to force users who use up/down with mouse clicks to press enter on the keyboard to submit the final result. This is not what a user expects.

  • "setKeyboardTracking(bool)" helped me working out this issue

  • @sayan275 Not clear why, but I think this does what you need:

    set the keyboardTracking property of the spinbox to false, then do the following:

    void MainWindow::on_mySpinBox_valueChanged(int arg1)
    void MainWindow::on_mySpinBox_editingFinished()
        // what you need to be done, on spinbox loosing focus or arrows adjusting (and not while editing)

Log in to reply