Qt find out if QSpinBox was changed by user

[亡魂溺海] 提交于 2020-12-30 07:28:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!