8.5.8.1.2. pymodaq_gui.utils.widget_sync.factories module
Factory methods for common widget types.
This module can be extended by users to add their own factory methods.
- class pymodaq_gui.utils.widget_sync.factories.WidgetSyncFactories[source]
Bases:
objectMixin class providing factory methods for common Qt widgets.
Users can create their own factory methods by: 1. Inheriting from WidgetSync 2. Adding their own @classmethod factories
Example
- class MyWidgetSync(WidgetSync):
@classmethod def for_my_custom_widget(cls, widget, initial=None):
return cls.for_property(widget, ‘customProperty’, ‘customSignal’, initial)
Methods
for_checkbox(checkbox[, initial, mode, ...])Create a sync for checkbox widgets.
for_combobox(combobox[, initial, use_text, ...])Create a sync for QComboBox widgets.
for_lineedit(lineedit[, initial, mode, ...])Create a sync for QLineEdit widgets.
for_property(widget, property_name[, ...])Create a sync for any Qt property with auto-detection.
for_slider(slider[, initial, mode, validator])Create a sync for QSlider widgets.
for_spinbox(spinbox[, initial, mode, validator])Create a sync for QSpinBox or QDoubleSpinBox widgets.
- classmethod for_checkbox(checkbox, initial=False, mode=None, validator=None)[source]
Create a sync for checkbox widgets.
- Parameters:
checkbox (
QCheckBox) – The first checkbox to syncinitial (bool) – Initial checked state (default: False)
mode (
SyncMode, optional) – Sync mode (default: BIDIRECTIONAL)validator (
collections.abc.Callable, optional) – Optional validator function
- Returns:
A new sync instance
- Return type:
WidgetSync
Example
>>> sync = WidgetSync.for_checkbox(my_checkbox, initial=True) >>> sync.add(another_checkbox)
- classmethod for_combobox(combobox, initial=0, use_text=False, mode=None, validator=None)[source]
Create a sync for QComboBox widgets.
- Parameters:
combobox (
QComboBox) – The first combobox to syncinitial (int | str) – Initial value - index if use_text=False, text if use_text=True (default: 0)
use_text (bool) – If True, sync currentText; if False, sync currentIndex (default: False)
mode (
SyncMode, optional) – Sync mode (default: BIDIRECTIONAL)validator (
collections.abc.Callable, optional) – Optional validator function
- Returns:
A new sync instance
- Return type:
WidgetSync
Examples
>>> # Sync by index (default) >>> sync = WidgetSync.for_combobox(combo1, initial=0) >>> sync.add(combo2)
>>> # Sync by text >>> sync = WidgetSync.for_combobox(combo1, initial="Option A", use_text=True) >>> sync.add(combo2)
- classmethod for_lineedit(lineedit, initial='', mode=None, validator=None)[source]
Create a sync for QLineEdit widgets.
- Parameters:
lineedit (
QLineEdit) – The first line edit to syncinitial (str) – Initial text (default: “”)
mode (
SyncMode, optional) – Sync mode (default: BIDIRECTIONAL)validator (
collections.abc.Callable, optional) – Optional validator function
- Returns:
A new sync instance
- Return type:
WidgetSync
Example
>>> sync = WidgetSync.for_lineedit(edit1, initial="Hello") >>> sync.add(edit2)
- classmethod for_property(widget, property_name, signal_name=None, initial=None, mode=None, data_type=None, validator=None)[source]
Create a sync for any Qt property with auto-detection.
This is the most flexible factory - all others build on this.
- Parameters:
widget (QWidget) – The first widget to sync
property_name (str) – Name of the Qt property (e.g., ‘checked’, ‘value’, ‘text’)
signal_name (str | None) – Name of the change signal. If None, auto-detects from property
initial (Any) – Initial value (if None, uses widget’s current value)
mode (SyncMode | None) – Sync mode (default: BIDIRECTIONAL)
data_type (type | None) – Explicit data type for type checking (default: inferred from initial)
validator (Any) – Optional validator function to correct/constrain values
- Returns:
A new sync instance with the widget connected
- Return type:
WidgetSync
Example
>>> sync = WidgetSync.for_property(my_spinbox, 'value', initial=50) >>> sync.add(other_spinbox) # Add more spinboxes
- classmethod for_slider(slider, initial=0, mode=None, validator=None)[source]
Create a sync for QSlider widgets.
- Parameters:
slider (
QSlider) – The first slider to syncinitial (int) – Initial value (default: 0)
mode (
SyncMode, optional) – Sync mode (default: BIDIRECTIONAL)validator (
collections.abc.Callable, optional) – Optional validator function
- Returns:
A new sync instance
- Return type:
WidgetSync
Example
>>> sync = WidgetSync.for_slider(slider1, initial=75) >>> sync.add(slider2)
- classmethod for_spinbox(spinbox, initial=0, mode=None, validator=None)[source]
Create a sync for QSpinBox or QDoubleSpinBox widgets.
- Parameters:
spinbox (
QSpinBox | QDoubleSpinBox) – The first spinbox to syncinitial (int | float) – Initial value (default: 0)
mode (
SyncMode, optional) – Sync mode (default: BIDIRECTIONAL)validator (
collections.abc.Callable, optional) – Optional validator function
- Returns:
A new sync instance
- Return type:
WidgetSync
Example
>>> sync = WidgetSync.for_spinbox(spinbox1, initial=50) >>> sync.add(spinbox2) >>> sync.add(spinbox3)