utils.py 631 B

123456789101112131415161718192021
  1. from pathlib import Path
  2. from PySide6.QtUiTools import QUiLoader
  3. from PySide6.QtCore import (
  4. QFile,
  5. QIODevice,
  6. )
  7. def load_ui(name, parent=None):
  8. ui_file_name = Path(__file__).parent / 'resources' / 'ui' / f"{name}.ui"
  9. ui_file = QFile(ui_file_name)
  10. if not ui_file.open(QIODevice.ReadOnly):
  11. raise RuntimeError(
  12. f"Cannot open {ui_file_name}: {ui_file.errorString()}")
  13. loader = QUiLoader()
  14. ui = loader.load(ui_file, parentWidget=parent)
  15. ui_file.close()
  16. if not ui:
  17. raise RuntimeError(
  18. f"Cannot load UI {ui_file_name}: {loader.errorString()}")
  19. return ui