qtqwidgetqspinbox

QSpinbox editingFInished signal on up/down arrow press


I went through a lot of posts regarding QSpinBox signals editingFinished and valueChanged, but not able solve my problem, ie,

  1. I want editingFinished signal to be emitted, when user enters any value and presses "Enter".[DONE]
  2. The user can change value with the up/down arrows either by keyPress or mousePress. This can be done by valueChange signal, but not editingFinished

So to achieve both, I'm facing problem, 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.

    connect(ui->spinBox, &QSpinBox::editingFinished,
            this, [&]()
    {
        ui->label->setText(QString::number(ui->spinBox->value()));
    });

But I want the up/down arrows to work also. so for this again valueChanged is required.

Is there any means to achieve it directly, rather than adding my own spinbox class and overriding the events for up/down arrow click/press.

Any suggestion is appreciated.


Solution

  • Not clear why, but 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)
    {
        on_mySpinBox_editingFinished();
    }
    
    void MainWindow::on_mySpinBox_editingFinished()
    {
        // what you need to be done, on spinbox loosing focus or arrows adjusting (and not while editing)
    }