Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt find out if QSpinBox was changed by user

Tags:

c++

qt

qspinbox

Let's suppose I have a QSpinBox, how I can find out if the value was changed manually from user or from a other function?

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

like image 637
Matthias Avatar asked Oct 14 '14 10:10

Matthias


1 Answers

Possible solution:

ui->spinBox->blockSignals(true);
ui->spinBox->setValue(50);
ui->spinBox->blockSignals(false);

In this case, signal will not be emitted, so all what you can catch by valueChanged() signal is only user's action.

For example:

void MainWindow::on_spinBox_valueChanged(int arg1)
{
    qDebug() << "called";
}

When user change value by mouse or type by keybord, you see "called", but when you setValue with blocking signals, you don't see "called".

Another approach is to provide some bool variable and set it to true before the setValue and check this variable in slot. If it is false(user action) - do some action, if not - don't do(change bool to false). Advantages: you don't block signal. Disadvantages: maybe hard readable code, if slot calls many times, you will many time do this unnecessary checking.

like image 112
Kosovan Avatar answered Nov 07 '22 17:11

Kosovan