load_ui.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) 2020 Riverbank Computing Limited.
  2. # Copyright (c) 2006 Thorsten Marek.
  3. # All right reserved.
  4. #
  5. # This file is part of PyQt.
  6. #
  7. # You may use this file under the terms of the GPL v3 or the revised BSD
  8. # license as follows:
  9. #
  10. # "Redistribution and use in source and binary forms, with or without
  11. # modification, are permitted provided that the following conditions are
  12. # met:
  13. # * Redistributions of source code must retain the above copyright
  14. # notice, this list of conditions and the following disclaimer.
  15. # * Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. # * Neither the name of the Riverbank Computing Limited nor the names
  20. # of its contributors may be used to endorse or promote products
  21. # derived from this software without specific prior written
  22. # permission.
  23. #
  24. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  35. def loadUiType(uifile):
  36. """loadUiType(uifile) -> (form class, base class)
  37. Load a Qt Designer .ui file and return the generated form class and the Qt
  38. base class.
  39. uifile is a file name or file-like object containing the .ui file.
  40. """
  41. import io
  42. import sys
  43. from PyQt6 import QtWidgets
  44. from .Compiler import compiler
  45. code_string = io.StringIO()
  46. winfo = compiler.UICompiler().compileUi(uifile, code_string)
  47. ui_globals = {}
  48. exec(code_string.getvalue(), ui_globals)
  49. uiclass = winfo["uiclass"]
  50. baseclass = winfo["baseclass"]
  51. # Assume that the base class is a custom class exposed in the globals.
  52. ui_base = ui_globals.get(baseclass)
  53. if ui_base is None:
  54. # Otherwise assume it is in the QtWidgets module.
  55. ui_base = getattr(QtWidgets, baseclass)
  56. return (ui_globals[uiclass], ui_base)
  57. def loadUi(uifile, baseinstance=None, package=''):
  58. """loadUi(uifile, baseinstance=None, package='') -> widget
  59. Load a Qt Designer .ui file and return an instance of the user interface.
  60. uifile is a file name or file-like object containing the .ui file.
  61. baseinstance is an optional instance of the Qt base class. If specified
  62. then the user interface is created in it. Otherwise a new instance of the
  63. base class is automatically created.
  64. package is the optional package which is used as the base for any relative
  65. imports of custom widgets.
  66. """
  67. from .Loader.loader import DynamicUILoader
  68. return DynamicUILoader(package).loadUi(uifile, baseinstance)