[PySide] Is dual inheritance clearer?
Goswin von Brederlow
goswin-v-b at web.de
Thu Jun 11 12:22:40 CEST 2015
On Thu, Apr 04, 2013 at 04:02:06PM +1100, Algis Kabaila wrote:
> Hi,
>
> I believe that PySide (and PyQt for that matter) are widely used for GUI
> design. There are two main "orderly" ways to start a GUI program, after
> designing it in the Qt Creator:
>
> (1)
> With dual inheritance:
> class MainWindow(QMainWindow, Ui_MainWindow):
> def __init__(self, parent=None):
> super(MainWindow, self).__init__(parent)
> self.setupUi(self)
>
> (2)
> Without dual inheritance:
> class MainWindow(QMainWindow):
> def __init__(self, parent=None):
> super(MainWindow, self).__init__(parent)
> self.ui = Ui_MainWindow
> self.ui.setupUi(Ui_MainWindow(),self)
>
> There have been some horror stories about the evils of multiple
> inheritance. What is the preferred method, (1) or (2)?
>
> Thank you in anticipation of your response.
>
> Al.
The PyQt/PySide classes are realy not setup for multiple inheritance
and if you have those dual inheritance somewhere in the middle of your
inheritance, i.e. if you inherit MainWindow again (which makes more
sense for other widgets) then things break, constructors don't get
called and you end up with unusable objects.
I prefer (python3, seriously still using python2? :):
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = loadUi("Widget.ui", self)
--- loadUi ---
I'm using the binding_helper.py (Copyright (c) 2011, Dirk Thomas,
Dorian Scholz, TU Darmstadt) which allows code to work with PyQt or
PySide automatically or selectively and provides loadUi for PySide.
Why should I add the extra step of generating python code form UI
files when I can just load the UI file directly? An extra benefit is
that one can edit UI files for subwindows or dialogs at runtime and
the changes appear the next time the subwindow or dialog is opened.
MfG
Goswin
More information about the PySide
mailing list