Qt finds out if QSpinBox has been modified by user
Possible Solution:
ui->spinBox->blockSignals(true);
ui->spinBox->setValue(50);
ui->spinBox->blockSignals(false);
In this case, the signal will not be emitted, so all that you can catch with the signal valueChanged()
is only user action.
For example:
void MainWindow::on_spinBox_valueChanged(int arg1)
{
qDebug() << "called";
}
When the user changes the value of the mouse or types keybord, you see "called"
, but when you are setValue
with blocking signals, you do not see "called"
.
Another approach is to provide some bool variable and set it true
before setValue
and check that variable in the slot. If it's false (user action) - do some action, if not - don't do it (change bool to false). Benefits: you don't block the signal. Disadvantages: Possibly hard-to-read code, if the slot is ringing many times, you will spend a lot of time doing this unnecessary check.
source to share