问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/26358945/qt-find-out-if-qspinbox-was-changed-by-user