diff --git a/python/gui/auto_generated/settings/qgssettingseditorwidgetwrapper.sip.in b/python/gui/auto_generated/settings/qgssettingseditorwidgetwrapper.sip.in index 745f87e11309..353748b91c93 100644 --- a/python/gui/auto_generated/settings/qgssettingseditorwidgetwrapper.sip.in +++ b/python/gui/auto_generated/settings/qgssettingseditorwidgetwrapper.sip.in @@ -10,6 +10,7 @@ + class QgsSettingsEditorWidgetWrapper : QObject { %Docstring(signature="appended") @@ -82,13 +83,17 @@ Sets the ``value`` of the widget The wrapper must be configured before calling this medthod %End - void enableAutomaticUpdate(); + void enableAutomaticUpdate( QDialog *dialog = 0 ); %Docstring -Enables automatic update -so that the setting gets update -on value change in the widget and -sets the widget to the current settings value. -This must called after createEditor or configureEditor. +Enables automatic update, which causes the setting to be updated immediately when the widget +value is changed. + +If a ``dialog`` is provided, the setting will be updated when the dialog is accpeted. +If not, the setting will be updated directly at each widget value change. + +.. note:: + + This must called after :py:func:`~QgsSettingsEditorWidgetWrapper.createEditor` or :py:func:`~QgsSettingsEditorWidgetWrapper.configureEditor`. .. versionadded:: 3.40 %End @@ -107,9 +112,8 @@ Configures an existing ``editor`` widget virtual void enableAutomaticUpdatePrivate() = 0; %Docstring -Enables automatic update -so that the setting gets update -on value change in the widget. +Enables automatic update, which causes the setting to be updated immediately when the widget +value is changed. .. versionadded:: 3.40 %End diff --git a/src/gui/settings/qgssettingseditorwidgetwrapper.cpp b/src/gui/settings/qgssettingseditorwidgetwrapper.cpp index ace0c73f5f2f..143c9090bf34 100644 --- a/src/gui/settings/qgssettingseditorwidgetwrapper.cpp +++ b/src/gui/settings/qgssettingseditorwidgetwrapper.cpp @@ -19,6 +19,7 @@ #include "qgslogger.h" #include "qgssettingsentry.h" +#include #include @@ -59,3 +60,19 @@ bool QgsSettingsEditorWidgetWrapper::configureEditor( QWidget *editor, const Qgs return ok; } + +void QgsSettingsEditorWidgetWrapper::enableAutomaticUpdate( QDialog *dialog ) +{ + setWidgetFromSetting(); + if ( dialog ) + { + QObject::connect( dialog, &QDialog::accepted, this, [ = ]() + { + setSettingFromWidget(); + } ); + } + else + { + enableAutomaticUpdatePrivate(); + } +} diff --git a/src/gui/settings/qgssettingseditorwidgetwrapper.h b/src/gui/settings/qgssettingseditorwidgetwrapper.h index 59f37a1642f5..f145723860cf 100644 --- a/src/gui/settings/qgssettingseditorwidgetwrapper.h +++ b/src/gui/settings/qgssettingseditorwidgetwrapper.h @@ -23,6 +23,8 @@ class QgsSettingsEntryBase; +class QDialog; + /** * \ingroup gui * \brief Base class for settings editor wrappers @@ -84,18 +86,14 @@ class GUI_EXPORT QgsSettingsEditorWidgetWrapper : public QObject * Enables automatic update, which causes the setting to be updated immediately when the widget * value is changed. * - * Calling this method will set the widget's current value to match the current settings value. + * If a \a dialog is provided, the setting will be updated when the dialog is accpeted. + * If not, the setting will be updated directly at each widget value change. * * \note This must called after createEditor() or configureEditor(). - * \warning Do NOT call this method from places where a widget is embedded in a dialog with a cancel button, as the auto-update logic will immediately overwrite the setting value and prevent rollback if the user cancels the dialog. * * \since QGIS 3.40 */ - void enableAutomaticUpdate() - { - setWidgetFromSetting(); - enableAutomaticUpdatePrivate(); - } + void enableAutomaticUpdate( QDialog *dialog = nullptr ); protected: