Skip to content

Instantly share code, notes, and snippets.

@masci
Created February 22, 2011 16:59
Show Gist options
  • Save masci/838979 to your computer and use it in GitHub Desktop.
Save masci/838979 to your computer and use it in GitHub Desktop.
Metaclass for automating uic operation when using the "Multiple Inheritance Approach" in Qt Widgets
from PyQt4 import uic
from PyQt4.QtCore import pyqtWrapperType
UI_DIR='path_to_ui_files'
class WindowMeta (pyqtWrapperType, type):
"""This is the metaclass of all Qt-Windows. It automatically
sets the correct base classes, so they do not have to be set
by the programmer.
@attention: The class has to have the same name as the .ui-file.
"""
def __new__ (cls, name, bases, dict):
# parse UI
new_bases = uic.loadUiType('%s/%s.ui' % (UI_DIR, name))
# all the base classes - e.g. (UI_AQtWindow, PyQt4.QtGui.QDialog)
dict.update(_bases = new_bases)
# only the Qt-Baseclass (like QDialog or QMainWindow)
# the class has to later to a self._qt_base.__init__(...)
dict.update(_qt_base = new_bases[1])
# create class
return super(WindowMeta, cls).__new__(cls, name, new_bases+bases, dict)
def __init__ (cls, name, bases, dict):
b = dict["_bases"]
del dict["_bases"]
# init class
super(WindowMeta, cls).__init__(name, b+bases, dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment