edifice.QtWidgetElement#

class edifice.QtWidgetElement(style=None, tool_tip=None, cursor=None, context_menu=None, css_class=None, size_policy=None, focus_policy=None, _focus_open=False, enabled=None, on_click=None, on_key_down=None, on_key_up=None, on_mouse_down=None, on_mouse_up=None, on_mouse_enter=None, on_mouse_leave=None, on_mouse_move=None, on_drop=None, on_resize=None)[source]#

Bases: Element, Generic[_T_widget]

Base Qt Widget.

All elements with an underlying QWidget inherit from this element.

The props add basic functionality such as styling and event handlers.

Parameters:
  • style (Optional[Mapping[str, Any]]) – Style dictionary. See Styling for details.

  • tool_tip (Optional[str]) – The tool tip displayed when hovering over the widget.

  • cursor (Optional[str]) – The shape of the cursor when mousing over this widget. Must be one of: "default", "arrow", "pointer", "grab", "grabbing", "text", "crosshair", "move", "wait", "ew-resize", "ns-resize", "nesw-resize", "nwse-resize", "not-allowed", "forbidden"

  • context_menu (Optional[Mapping[str, Union[None, Callable[[], Any], Mapping[str, Union[None, Callable[[], Any], ContextMenuType]]]]]) – The context menu to display when the user right clicks on the widget. Expressed as a dict mapping the name of the context menu entry to either a function (which will be called when this entry is clicked) or to another sub context menu. For example, {"Copy": copy_fun, "Share": {"Twitter": twitter_share_fun, "Facebook": facebook_share_fun}}

  • css_class (Optional[Any]) –

    A string or a list of strings, which will be stored in the css_class property of the Qt Widget. This can be used in an application stylesheet, like:

    QLabel[css_class=”heading”] { font-size: 18px; }

  • size_policy (Optional[QSizePolicy]) – Horizontal and vertical resizing policy, of type QSizePolicy

  • focus_policy (Optional[FocusPolicy]) –

    The various policies a widget can have with respect to acquiring keyboard focus, of type FocusPolicy.

    See also QWidget.focusPolicy.

  • _focus_open (bool) –

    Whether the Element should be focused when this Element is first rendered.

    This argument is not a prop and will only focus the Element one time on the first render.

  • enabled (Optional[bool]) – Whether the widget is enabled. If not, the widget will be grayed out and not respond to user input.

  • on_click (Optional[Callable[[QMouseEvent], Optional[Awaitable[None]]]]) – Callback for click events (mouse pressed and released). Takes a QMouseEvent as argument.

  • on_key_down (Optional[Callable[[QKeyEvent], Optional[Awaitable[None]]]]) – Callback for key down events (key pressed). Takes a QKeyEvent as argument. The key() method of QKeyEvent returns the Qt.Key pressed. The text() method returns the unicode of the key press, taking modifier keys (e.g. Shift) into account.

  • on_key_up (Optional[Callable[[QKeyEvent], Optional[Awaitable[None]]]]) –

    Callback for key up events (key released). Takes a QKeyEvent as argument.

  • on_mouse_down (Optional[Callable[[QMouseEvent], Optional[Awaitable[None]]]]) –

    Callback for mouse down events (mouse pressed). Takes a QMouseEvent as argument.

  • on_mouse_up (Optional[Callable[[QMouseEvent], Optional[Awaitable[None]]]]) –

    Callback for mouse up events (mouse released). Takes a QMouseEvent as argument.

  • on_mouse_enter (Optional[Callable[[QMouseEvent], Optional[Awaitable[None]]]]) –

    Callback for mouse enter events (triggered once every time mouse enters widget). Takes a QMouseEvent as argument.

  • on_mouse_leave (Optional[Callable[[QMouseEvent], Optional[Awaitable[None]]]]) –

    Callback for mouse leave events (triggered once every time mouse leaves widget). Takes a QMouseEvent as argument.

  • on_mouse_move (Optional[Callable[[QMouseEvent], Optional[Awaitable[None]]]]) –

    Callback for mouse move events (triggered every time mouse moves within widget). Takes a QMouseEvent as argument.

  • on_drop (Optional[Callable[[QDragEnterEvent | QDragMoveEvent | QDragLeaveEvent | QDropEvent], None]]) –

    Handle drop events.

    See Dropping.

    The handler function will be passed one of

    The handler function should handle all cases. Example:

    dropped_files, dropped_files_set = use_state(cast(list[str], []))
    proposed_files, proposed_files_set = use_state(cast(list[str], []))
    
    def handle_on_drop(event: QDragEnterEvent | QDragMoveEvent | QDragLeaveEvent | QDropEvent):
        event.accept()
        match event:
            case QDragEnterEvent():
                # Handle proposed drop enter
                if event.mimeData().hasUrls():
                    event.acceptProposedAction()
                    proposed_files_set([url.toLocalFile()) for url in event.mimeData().urls()])
            case QDragMoveEvent():
                # Handle proposed drop move
                if event.mimeData().hasUrls():
                    event.acceptProposedAction()
            case QDragLeaveEvent():
                # Handle proposed drop leave
                proposed_files_set([])
            case QDropEvent():
                # Handle finalized drop
                if event.mimeData().hasUrls():
                    dropped_files_set(proposed_files)
                    proposed_files_set([])
    

    Note that the handler function cannot not be a coroutine.

  • on_resize (Optional[Callable[[QResizeEvent], Optional[Awaitable[None]]]]) – Callback for resize events. Takes a QResizeEvent as argument.

Methods

__init__([style, tool_tip, cursor, ...])

register_ref(reference)

Registers provided Reference to this Element.

set_key(key)

Set the key of an Element.

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.

underlying: Optional[TypeVar(_T_widget, bound= QWidget)]#

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