edifice.CustomWidget#

class edifice.CustomWidget(*args, **kwargs)[source]#

Bases: QtWidgetElement[_T_customwidget]

Custom widgets that you can define.

Not all widgets are currently supported by Edifice. You can create your own base Qt Widget element by inheriting from QtWidgetElement directly, or more simply by overriding CustomWidget:

class MyWidgetElement(CustomWidget[QtWidgets.FooWidget]):

    def __init__(self, text="", value=0, **kwargs):
        super().__init__(**kwargs)
        self._register_props(
            {
                "text": text,
                "value": value,
            }
        )

    def create_widget(self):
        # This function should return the new widget
        # (with parent set to None; Edifice will handle parenting)
        return QtWidgets.FooWidget()

    def update(self, widget: QtWidgets.FooWidget, diff_props: PropsDiff):
        # This function should update the widget
        match diff_props.get("text"):
            case _propold, propnew:
                widget.setText(propnew)
        match diff_props.get("value"):
            case _propold, propnew:
                widget.setValue(propnew)

The two methods to override are CustomWidget.create_widget(), which should return the Qt widget, and CustomWidget.update(), which takes the current widget and props diff, and should update the widget according to the new props. The custom widget inherits from QtWidgetElement, allowing the user to, for example, set the style.

Methods

__init__(*args, **kwargs)

create_widget()

Create the widget on first render.

register_ref(reference)

Registers provided Reference to this Element.

set_key(key)

Set the key of an Element.

update(widget, diff_props)

Update the widget based on the diff_props.

Attributes

children

The children of this Element.

props

The props of this Element.

underlying

The underlying QWidget, which may not exist if this Element has not rendered.

create_widget()[source]#

Create the widget on first render.

Return type:

TypeVar(_T_customwidget, bound= QWidget)

update(widget, diff_props)[source]#

Update the widget based on the diff_props.