Qt finds out if QSpinBox has been modified by user

Suppose I have QSpinBox

, how can I tell if the value has been changed manually from the user or from another function?

EDIT: I want to do some actions only when the user changes values, but if your program does this (setValue), I don't want to do those actions.

+3


source to share


1 answer


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.

+8


source







All Articles