The widgets.factory sub-package#

The pydidas.widgets.factory subpackage includes factory functions for widget creation. It also includes the CreateWidgetsMixin class which allows other classes easy access to simplified widget creation.

class pydidas.widgets.factory.CreateWidgetsMixIn

Bases: object

A mixin class to allow easy widget creation in their host classes.

The CreateWidgetsMixIn class includes methods for easy adding of widgets to the layout. The create_something methods from the factories are called and in addition, the layout and positions can be set.

Use the “gridPos” keyword to define the widget position in the parent’s layout.

add_any_widget(ref: str | None, widget_instance: QWidget, **kwargs: dict)

Add any existing widget to the layout and apply settings to it.

Parameters:
  • ref (Union[str, None]) – The widget reference key.

  • widget_instance (QWidget) – The widget instance.

  • **kwargs (dict) – Any attributes supported by the specific QWidget with a setAttribute method are valid kwargs. In addition, ‘layout_kwargs’ is a valid key to pass a dictionary with attributes for the widget’s layout.

create_any_widget(ref: str | None, widget_class: type, *args: Tuple, **kwargs: Dict)

Create any widget with any settings and add it to the layout.

Note

Widgets must support generic args and kwargs arguments. This means that generic PyQt widgets cannot be created using this method. They can be added, however, using the add_any_widget method.

Parameters:
  • ref (Union[str, None]) – The reference name in the _widgets dictionary.

  • widget_class (type) – The class type of the widget.

  • *args (args) – Any arguments for the widget creation.

  • **kwargs (dict) – Keyword arguments for the widget creation.

Raises:

TypeError – If the reference “ref” is not of type string.

create_button(ref: str | None, text: str, **kwargs: Dict)

Create a QPushButton and store the widget.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • **kwargs (dict) – Any attributes supported by QPushButton with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the button’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ keywords can be used to control the font properties. The button’s clicked method can be connected directly, by specifing the slot through the ‘clicked’ kwarg.

create_check_box(ref: str | None, text, **kwargs: Dict)

Create a PydidasCheckBox and store the widget.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • text (str) – The CheckBox’s descriptive text.

  • **kwargs (dict) – Any attributes supported by QCheckBox with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QProgressBar’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ can be used to control the font properties or generic Qt properties.

create_combo_box(ref: str | None, **kwargs: Dict)

Create a PydidasComboBox and store the widget.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • **kwargs (dict) – Any attributes supported by QComboBox with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QComboBox’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ can be used to control the font properties or generic Qt properties.

create_empty_widget(ref: str | None, **kwargs: Dict)

Create an ampty QWidget with a agrid layout.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • **kwargs (dict) – Any attributes supported by the generic QWidget with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QWidget’s position in its parent’s layout.

create_label(ref: str | None, text: str, **kwargs: dict)

Create a PydidasLabel and store the widget.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • text (str) – The label’s displayed text.

  • **kwargs (dict) – Any attributes supported by QLabel with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the label’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ keywords can be used to control the font properties.

create_line(ref: str | None, **kwargs: Dict)

Create a line widget.

This method creates a line widget (implemented as flat QFrame) as separator and adds it to the parent widget.

Parameters:

**kwargs (dict) – Any additional keyword arguments. All QFrame attributes with setAttribute implementation are valid kwargs.

create_lineedit(ref: str | None, **kwargs: Dict)

Create a PydidasLineEdit and store the widget.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • **kwargs (dict) – Any attributes supported by QLineEdit with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the LineEdit’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ keywords can be used to control the font properties.

create_progress_bar(ref: str | None, **kwargs: Dict)

Create a QProgressBar and store the widget.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • **kwargs (dict) – Any attributes supported by QProgressBar with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the QProgressBar’s position in its parent’s layout.

create_radio_button_group(ref: str | None, entries: List, **kwargs: Dict)

Create a RadioButtonGroup widget and set its properties.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • text (str) – The CheckBox’s descriptive text.

  • **kwargs (dict) – Any attributes supported by the generic QWidget with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the RadioButtonGroup’s position in its parent’s layout. The ‘fontsize_offset’, ‘bold’, ‘italic’, ‘underline’ can be used to control the font properties or generic Qt properties. The ‘entries’ keyword takes a list of entries for the buttons and the number of rows and columns can be specified with the ‘rows’ and ‘columns’ keywords, respectively.

create_spacer(ref: str | None, **kwargs: Dict)

Create a QSpacerItem and set its properties.

Parameters:
  • ref (Union[str, None]) – The widget reference string. If None, an internal reference will be used.

  • **kwargs (dict) – Any attributes supported by QSpacerItem with a setAttribute method are valid kwargs. In addition, the gridPos key allows to specify the spacer’s position in its parent’s layout.

create_spin_box(ref: str | None, **kwargs: Dict)

Create a QSpinBox and store the widget.

Parameters:
  • ref (Union[str, None]) – The reference string for storing the widget.

  • **kwargs (dict) – Any attributes supported by QSpinBox with a setAttribute method are valid kwargs. In addition, the ‘gridPos’ keyword can be used to specify the SpinBox’s position in its parent’s layout. The default range is set to (0, 1), if it is not overridden with the ‘range’ keyword.

class pydidas.widgets.factory.EmptyWidget(**kwargs: dict)

Bases: PydidasWidgetMixin, QWidget

Create an empty widget with a QGridLayout.

The constructor also sets QProperties based on the supplied keywords, if a matching setter was found.

class pydidas.widgets.factory.PydidasCheckBox(*args: tuple, **kwargs: dict)

Bases: PydidasWidgetMixin, QCheckBox

Create a QCheckBox with automatic font formatting.

class pydidas.widgets.factory.PydidasComboBox(*args: tuple, **kwargs: dict)

Bases: PydidasWidgetMixin, QComboBox

Create a QComboBox with automatic font formatting.

This Combobox overwrites the default sizeHint to take as much space (up to the generic IO widget width) to fill the layout.

class pydidas.widgets.factory.PydidasLabel(*args: tuple, **kwargs: dict)

Bases: PydidasWidgetMixin, QLabel

Create a QLabel with automatic font formatting.

class pydidas.widgets.factory.PydidasLineEdit(*args: tuple, **kwargs: dict)

Bases: PydidasWidgetMixin, QLineEdit

Create a QLineEdit with automatic font formatting.

update_size_hint(new_size_hint: QSize)

Update the sizeHint value of this widget.

Parameters:

new_size_hint (QtCore.QSize) – The new size hint.

class pydidas.widgets.factory.PydidasPushButton(*args: tuple, **kwargs: dict)

Bases: PydidasWidgetMixin, QPushButton

Create a QPushButton with automatic font and size formatting.

class pydidas.widgets.factory.RadioButtonGroup(entries, **kwargs)

Bases: QWidget

The RadioButtonGroup holds a number of QRadioButtons in a QButtonGroup.

Creation is automated based on the entries.

property active_label: str

Return the label name of the currently selected button.

Returns:

The label.

Return type:

str

property emit_signal: bool

Query whether a signal will be emitted on change.

Returns:

Flag whether a signal will be emitted.

Return type:

bool

process_new_font_metrics(font_width: float, font_height: float)

Adjust the widget height based on the font metrics.

Parameters:
  • font_width (float) – The font width in pixels.

  • font_height (float) – The font height in pixels.

select_by_index(index: int)

Select and activate a new RadioButton based on the index.

Parameters:

index (int) – The new RadioButton’s index.

select_by_label(label: str)

Select and activate a new RadioButton based on the text label.

Parameters:

label (str) – The new RadioButton’s label.

which_is_checked() int

Return the information about which index is checked.

Returns:

The active RadioButton’s index.

Return type:

int

class pydidas.widgets.factory.SquareButton(*args: tuple, **kwargs: dict)

Bases: PydidasWidgetMixin, QPushButton

A PushButton which tries to stay square in size.

heightForWidth(width: int) int

Get the same preferred height as the width.

Parameters:

width (int) – The widget width.

Returns:

The width.

Return type:

int

process_new_font_metrics(font_width: float, font_height: float)

Set the fixed width of the widget dynamically from the font metrics.

Parameters:
  • font_width (float) – The font width in pixels.

  • font_height (float) – The font height in pixels.

sizeHint()

Set the sizeHint based on the font size.