edifice.Reference#

class edifice.Reference[source]#

Bases: Generic[_T_Element]

Reference to an Element for imperative commands.

Edifice tries to abstract away the need to issue imperative commands to widgets but this is not always possible and not every feature of Qt Widgets is supported by Edifice. In some cases, we might need to issue imperative commands to the Elements and their underlying Qt Widgets. Reference gives us access to a rendered Element.

Create a Reference with the use_ref() Hook.

Element.register_ref() registers the Reference object to the Element.

Reference can be dereferenced by calling it. An instance of type Reference[Label] will dereference to an instance of Label when called.

Initially, a Reference object will dereference to None. After the first render, it will dereference to the rendered Element. When the rendered Element dismounts, the reference will once again dereference to None. Reference is valid whenever it is not None. Reference will evaluate false if the Element is None.

Access the QWidget underlying a QtWidgetElement, through the underlying attribute of the QtWidgetElement. use_effect() Hooks always run after the Elements are fully rendered.

@component
def MyComp(self):
    label_ref:Reference[Label] = use_ref()

    def did_render():
        label = label_ref()
        if label and label.underlying:
            # Set the text of the Label’s underlying QLabel widget.
            label.underlying.setText("After")

    use_effect(did_render, ())

    with VBoxView():
        Label("Before").register_ref(label_ref)

Methods