There are a few ways for PyQt4 to be inherited by the class that is being constructed and until recently I hadn’t noticed any different between one method over the other.
One way I used to load in the ui files was to convert them to a py file and then import them as a dual inheritance as such:
from PyQt4 import QtGui, QtCore
import from UI_window import ui_window
class window(QtQui.QMainWindow, ui_window):
def __init__(self, parent = None):
super(window, self).__init__(parent)
#or
QtGui.QMainWindow.__init__(self)
More recently I’ve been using the uic loader and loading in the .ui file from designer directly. This is nicer because I can skip the entire convert step, and it’s less files to worry about. That became:
from PyQt4 import QtGui, QtCore, uic
class mosaicGenerator(QtGui.QMainWindow):
def __init__(self, parent = None):
super(mosaicGenerator, self).__init__(parent)
self.ui = uic.loadUi("mosaicGenWindow.ui")
This was working great for a while until I found myself needing to make a new widget again. Everything worked, but it never showed up in the parent window. (it even would make space for it in the parent window) just nothing showed up. Testing again with the first method, the widget would load up fine in the parent, so it became something the uic.
There is another way to use the uic which is the uic.loadUiType() and that returns both your base class and a window class.
from PyQt4 import QtGui, QtCore, uic
tilesWidgetForm, baseClass = uic.loadUiType("addTilesWidget.ui")
class window(baseClass, tilesWidgetForm):
def __init__(self, parent = None):
super(window,self).__init__(parent)
self.setupUi(self)
And now my widget would load again in my parent window. While all methods will work, here is one place where I found requires one type of inheritance over another. There is, however, a difference over loading time and memory usage between the different types but that wasn’t the scope of this post.