Base Elements#
Base Elements are the building blocks for an Edifice application.
Each Base Element manages an underlying QWidget.
All Base Elements in this module inherit from QtWidgetElement
and its props.
This means that all Base Elements can be made to respond to clicks by passing
a handler function to the on_click
prop. All Base Elements are
stylable using the style
prop, see Styling.
|
Base Qt Widget Element. |
Base Elements can roughly be divided into Root Elements, Layout Elements, and Content Elements.
Root Base Elements#
The root parent Element of every Edifice application is a Window
.
Or an ExportList
for exporting components to
a normal PySide6/PyQt6 app.
|
The root |
The root element for an App which does |
Layout Base Elements#
Each Layout Base Element manages an underlying
QLayout,
and can have children (like the <div>
HTML tag).
To render a Layout Base Element, always use the with
statement
and indent the element’s children:
with VBoxView():
with HBoxView():
Label("Hello")
Label("World")
All of the Layout Base Elements have the name suffix View
.
|
Vertical column layout view for child elements. |
|
Horizontal row layout view for child elements. |
|
View layout for child widgets with fixed position. |
|
Scrollable vertical column layout. |
|
Scrollable horizontal row layout widget. |
|
Scrollable layout widget for child widgets with fixed position. |
|
Layout widget with multiple tabs. |
|
Grid layout widget for rendering children on a 2D rectangular grid. |
|
Table-style grid layout displays its children as aligned rows of columns. |
Row Element of a |
|
|
Flow-style layout. |
|
Pop-up Window layout. |
Content Base Elements#
Content Base Elements render a single Qt Widget and mostly do not have children.
|
Basic button widget. |
|
Button with child layout. |
|
Checkbox widget. |
|
Dropdown selection widget. |
|
Render an image. |
|
Render an SVG image. |
|
Display an Icon. |
|
Display an Icon Button. |
|
Text display widget. |
|
Progress bar widget. |
|
Radio buttons. |
|
Scroll bar widget. |
|
Slider bar widget. |
|
Widget for a |
|
One line text input widget. |
|
Multiline text input widget. |
Events#
Each user interaction with a Base Element generates events, and you can specify how to handle the event by providing a handler function.
These handlers are passed into the Base Element as props, for example
the on_click
prop of QtWidgetElement
,
or the on_change
prop for checkboxes, radio buttons, sliders,
and text input.
When the event occurs the handler function will be called with an argument
describing the event.
Usually, what you want to do in an event handler is to update some
state with a use_state()
setter function.
Async Event Handlers#
These event handlers run in the main event loop. A lengthy operation will block the application from interacting with the user, which is generally a bad user experience.
Consider this code.
@component
def MyComponent(self):
results, set_results = use_state("")
def on_click(event:QMouseEvent):
r = fetch_from_network()
set_results(r)
with VBoxView():
Button("Fetch", on_click=on_click)
if results:
Label(results)
When the Fetch button is clicked, the event handler will call a fetch_from_network
function,
blocking the application for an unbounded length of time.
If the user clicks the increment button while waiting for the fetch then nothing will happen.
Use the use_async_call
Hook for an asynchronous event handler
which will cancel automatically when MyComponent
is unmounted, or
when the Fetch button is pressed again.
@component
def MyComponent(self):
results, set_results = use_state("")
loading, set_loading = use_state(False)
async def on_click(event:QMouseEvent):
set_loading(True)
r = await fetch_from_network()
set_results(r)
set_loading(False)
on_click_handler, cancel_click_handler = use_async_call(on_click)
with VBoxView():
Button("Fetch", on_click=on_click_handler)
if loading:
Label("Fetching...")
elif results:
Label(results)
Custom Base Elements#
Not all QtWidgets are supported by Edifice.
Edifice provides CustomWidget
to allow you to bind arbitrary
QtWidgets to an Edifice Element.
|
Custom widgets that you can define. |