edifice.App#
- class edifice.App(root_element, inspector=False, create_application=True, application_name=None, qapplication=None)[source]#
Bases:
object
The main application object.
Start#
To start the application, call the
App.start()
method:App(MyRootElement()).start()
If the application is normal application in an operating system window, then the root Element rendered by
MyRootElement
must be aWindow
.Create the application event loop qasync.QEventLoop.
Start the application event loop.
Render the root Element.
Stop#
When the user closes the
Window
or whenApp.stop()
is called, the application will stop.Stopping the application will:
Unmount all Elements.
Cancel all
edifice.use_async()
tasks.Wait for the
edifice.use_async()
tasks to cancel.Stop the application event loop.
Close the application event loop.
Exit.
Export Widgets#
If you just want to create widgets that you’ll integrate with an existing Qt application, use the
App.export_widgets()
method instead ofApp.start()
. These widgets can then be plugged into the rest of your Qt application, and there’s no need to manage the rendering of the widgets — state changes will trigger automatic re-render without any intervention.Caveat: Your Qt application must use a qasync.QEventLoop.
Logging#
To enable Edifice logging set the logging level. Example:
import logging logger = logging.getLogger("Edifice") logger.setLevel(logging.INFO)
App Construction#
By default,
App()
creates a new QtWidgets.QApplication instance.- type root_element:
- param root_element:
the root Element of the application. It must render to an instance of
Window
orExportList
.- type inspector:
bool
- param inspector:
whether or not to run an instance of the Edifice Inspector alongside the main app. Defaults to False
- type create_application:
bool
- param create_application:
(default True) whether or not to create an instance of QApplication. Usually you want to use the default setting. However, if the QApplication is already created (e.g. in a test suite or if you just want Edifice to make a widget to plug into an existing Qt application), you can set this to False.
- type application_name:
Optional
[str
]- param application_name:
DEPRECATED the Qt application name to set when creating a new QApplication. This option is only relevant if create_application is True.
- type qapplication:
Optional
[QApplication
]- param qapplication:
(default None) The QtWidgets.QApplication. If you do not provide one, it will be created for you.
Methods
__init__
(root_element[, inspector, ...])Exports the underlying Qt
QWidgets
s from the Edifice Elements in theExportList
.set_stylesheet
(stylesheet)Adds a global stylesheet for the app.
start
()Start the application event loop.
Start the application event loop.
stop
()Stop the application.
- export_widgets()[source]#
Exports the underlying Qt
QWidgets
s from the Edifice Elements in theExportList
.Returns a list of QtWidgets.QWidget.
These
QWidget
s are still managed by Edifice, they will still benefit from full reactivity and state consistency. You can mount these widgets to your pre-existing Qt application this way:# Suppose parent_widget is defined in Qt code. @component def export_elements(self): with ExportList(): MyAwesomeComponent() AnotherComponent() edifice_app = edifice.App(export_elements(), create_application=False) edifice_widgets = edifice_app.export_widgets() edifice_widgets[0].setParent(parent_widget) parent_widget.layout().add_widget(edifice_widgets[0]) edifice_widgets[1].setParent(parent_widget) parent_widget.layout().add_widget(edifice_widgets[1])
- Return type:
list
[QWidget
]
- set_stylesheet(stylesheet)[source]#
Adds a global stylesheet for the app.
See Qt Style Sheets.
- Parameters:
stylesheet (
str
) – String containing the contents of the stylesheet- Return type:
- Returns:
self
- start_loop()[source]#
Start the application event loop.
A context manager alternative to
App.start()
which allows access to the application’s qasync.QEventLoop after the application starts, and before the first render.The
QEventLoop
is the asyncio current event loop. You can also access the asyncio current event loop in the usual way with asyncio.get_running_loop().In this example, we add a Unix
SIGINT
handler which willApp.stop()
the application:app = edifice.App(MyAppElement()) with app.start_loop() as loop: loop.add_signal_handler(signal.SIGINT, app.stop)
- Return type:
Generator
[QSelectorEventLoop
,None
,None
]