Last active
May 30, 2016 11:23
-
-
Save fredrikaverpil/a70f38c8b04c49aaf73c1d22846f924a to your computer and use it in GitHub Desktop.
PyQt/PySide .ui loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uifileloader | |
# Load Qt bindings via Qt wrapper | |
# Source: https://github.com/mottosso/Qt | |
from Qt import __binding__ | |
UI_FILE = 'my_ui.ui' | |
# form and base from uifileloader | |
form, base = uifileloader.uifileloader().load_ui_type( | |
UI_FILE=MY_UI_FILE | |
) | |
class MyUI(form, base): | |
def __init__(self, parent=None): | |
super(MyUI, self).__init__(parent) | |
# Set up UI | |
if __bindings__.startswith('PySide'): | |
self.setupUi(self) | |
if __bindings__.startswith('PyQt'): | |
uic.loadUi(MY_UI_FILE, self) | |
# Access UI and modify it | |
self.setObjectName('helloWorld') | |
self.setWindowTitle('Hello world') | |
# Example: | |
# self.listWidget.addItem('Hello world') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.etree.ElementTree as xml | |
from cStringIO import StringIO | |
# Load Qt bindings via Qt wrapper | |
# Source: https://github.com/mottosso/Qt | |
from Qt import QtCore | |
from Qt import QtWidgets | |
from Qt import __binding__ | |
if __binding__.startswith('PySide'): | |
from Qt import QtUiTools | |
import pysideuic | |
elif __binding__.startswith('PyQt'): | |
from Qt import uic | |
import sip | |
# Maya | |
try: | |
import maya.OpenMayaUI as omui | |
import shiboken | |
except ImportError: | |
pass | |
class PyQtFixer(QtWidgets.QMainWindow): | |
def __init__(self, parent=None): | |
"""Super, loadUi, signal connections | |
""" | |
super(PyQtFixer, self).__init__(parent) | |
print 'Making a detour (hack), necessary for when using PyQt' | |
class uifileloader(object): | |
def __init__(self, parent=None): | |
"""Super, loadUi, signal connections""" | |
super(uifileloader, self).__init__ | |
def load_ui_type(self, UI_FILE): | |
""" Pyside lacks the "load_ui_type" command, so we have to convert the ui file | |
to py code in-memory first and then execute it in a special frame to | |
retrieve the form_class. | |
""" | |
parsed = xml.parse(UI_FILE) | |
widget_class = parsed.find('widget').get('class') | |
form_class = parsed.find('class').text | |
with open(UI_FILE, 'r') as f: | |
o = StringIO() | |
frame = {} | |
if __binding__.startswith('PySide'): | |
pysideuic.compileUi(f, o, indent=0) | |
pyc = compile(o.getvalue(), '<string>', 'exec') | |
exec pyc in frame | |
# Fetch the base_class and form class based on their type | |
# in the xml from designer | |
form_class = frame['Ui_%s' % form_class] | |
base_class = eval('QtWidgets.%s' % widget_class) | |
if __binding__.startswith('PyQt'): | |
form_class = PyQtFixer | |
base_class = QtWidgets.QMainWindow | |
return form_class, base_class | |
def wrap_instance(self, ptr, base=None): | |
""" | |
Utility to convert a pointer to a Qt class instance (PySide/PyQt | |
compatible) | |
Source: http://nathanhorne.com/pyqtpyside-wrap-instance/ | |
:param ptr: Pointer to QObject in memory | |
:type ptr: long or Swig instance | |
:param base: (Optional) Base class to wrap with (Defaults to QObject, | |
which should handle anything) | |
:type base: QtWidgets.QWidget | |
:return: QWidget or subclass instance | |
:rtype: QtWidgets.QWidget | |
""" | |
if ptr is None: | |
return None | |
ptr = long(ptr) # Ensure type | |
if 'shiboken' in globals(): | |
if base is None: | |
qObj = shiboken.wrapInstance(long(ptr), QtCore.QObject) | |
metaObj = qObj.metaObject() | |
cls = metaObj.className() | |
superCls = metaObj.superClass().className() | |
if hasattr(QtWidgets, cls): | |
base = getattr(QtWidgets, cls) | |
elif hasattr(QtWidgets, superCls): | |
base = getattr(QtWidgets, superCls) | |
else: | |
base = QtWidgets.QWidget | |
return shiboken.wrapInstance(long(ptr), base) | |
elif 'sip' in globals(): | |
base = QtCore.QObject | |
return sip.wrapinstance(long(ptr), base) | |
else: | |
return None | |
def maya_main_window(self): | |
""" Returns the main Maya window. This works for both PySide and PyQt | |
since it uses the custom wrap_instance function. """ | |
main_window_ptr = omui.MQtUtil.mainWindow() | |
maya_window = self.wrap_instance(long(main_window_ptr), | |
QtWidgets.QWidget) | |
return maya_window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment