-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent signal recursion when canceling group change #88
Conversation
wyu71
commented
Feb 5, 2025
- Block signals when resetting combobox text to avoid triggering another selectedGroupChanged event.
Are you having issue because of this? I think line 199 already prevent this. |
This is primarily a performance optimization. While the extra signal wouldn't cause bugs (since the group hasn't changed), it would trigger unnecessary signal processing. The fix ensures that when we reset the combobox text after cancellation, we don't waste CPU cycles on processing a redundant signal. |
In complex composed widget I think it's risky and make a dependency on how QWidget currently implement the widget. For example, QComboBox has model, hidden internal line edit and how it really works purely relies on the implementation of QWidget. What we really want, is to prevent the signal-slot only in our code
From firing. SignalBlocker is doing the thing in a much larger scope. I would prefer, to have a bool flag and check it in selectedGroupChanged to prevent the actual handling. |
I would argue that the current approach using blockSignals() is actually more appropriate here:
The current solution is both clear in intent and correct in implementation. While a boolean flag might seem simpler, it could actually introduce more complexity and potential issues in a multi-signal environment. |
@wyu71
If you block signal on MyWidget, it's internal implementation my ends up in an inconsistent state. While your code may work today, it actually adds a hidden assumption that QComboBox will not change it's implementation to rely on signal from itself. This is what I try to avoid so we are less likely to have unintentional breakage. I would, rather do "disconnect()" "set()", "connect()", or put a bool value to implement my own block logic. Ideally, if QMetaObject::Connection can provides a way to block signal on a per-connection basis, I would use that. However, block all signal on an object, unless I own the full implementation of that object class, I would try to avoid using blockSignals on it. |
7f03c12
to
a5e1f69
Compare
Use explicit disconnect/connect of the signal handler when resetting combobox text to prevent recursion in selectedGroupChanged.
I understand your concern about using blockSignals() on Qt widgets. Let me propose a better solution using the disconnect/connect approach: void IMPage::selectedGroupChanged() {
if (config_->currentGroup() ==
ui_->inputMethodGroupComboBox->currentText()) {
return;
}
if (!config_->currentGroup().isEmpty() && config_->needSave()) {
if (QMessageBox::No ==
QMessageBox::question(this, _("Current group changed"),
_("Do you want to change group? Changes to "
"current group will be lost!"))) {
// Temporarily disconnect the signal
bool connection = disconnect(ui_->inputMethodGroupComboBox,
&QComboBox::currentTextChanged, this,
&IMPage::selectedGroupChanged);
ui_->inputMethodGroupComboBox->setCurrentText(
config_->currentGroup());
// Restore the connection
if (connection) {
connect(ui_->inputMethodGroupComboBox,
&QComboBox::currentTextChanged, this,
&IMPage::selectedGroupChanged);
}
return;
}
}
config_->setCurrentGroup(ui_->inputMethodGroupComboBox->currentText());
} |
@wengxt Please review it |