"""PyQt5 helper functions."""
import logging
import sys
from functools import wraps
from PyQt5 import QtWidgets
_logger = logging.getLogger(__name__)
[docs]
def get_main_window():
"""Get the Main window of a QT application."""
app = QtWidgets.QApplication.instance()
return [w for w in app.topLevelWidgets() if isinstance(w, QtWidgets.QMainWindow)][0]
[docs]
def create_app():
"""Create a Qt application."""
global QT_APP
QT_APP = QtWidgets.QApplication.instance()
if QT_APP is None: # pragma: no cover
QT_APP = QtWidgets.QApplication(sys.argv)
return QT_APP
[docs]
def require_qt(func):
"""Function decorator to specify that a function requires a Qt application.
Use this decorator to specify that a function needs a running Qt application before it can run.
An error is raised if that is not the case.
"""
@wraps(func)
def wrapped(*args, **kwargs):
if not QtWidgets.QApplication.instance():
_logger.warning('Creating a Qt application.')
create_app()
return func(*args, **kwargs)
return wrapped
[docs]
@require_qt
def run_app(): # pragma: no cover
"""Run the Qt application."""
global QT_APP
return QT_APP.exit(QT_APP.exec_())