[PySide] Is dual inheritance clearer?
Nik Melchior
nmelchior at seegrid.com
Thu Apr 4 23:09:50 CEST 2013
On Thu, Apr 04, 2013 at 08:10:29AM +0300, anatoly techtonik wrote:
> On Thu, Apr 4, 2013 at 8:02 AM, Algis Kabaila <akabaila at pcug.org.au> 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)?
> >
>
> I'd say - that depends on what Ui_MainWindow is, but in general case (2) is
> better, because:
>
> * self.ui defines a namespace to look for methods of UiMW object
> * explicit is better than implicit
I'm sorry this doesn't help resolve your confusion, but I prefer the opposite
approach, due to:
* Consistency with hand-written UI: When I write simple applications or
widgets that compose other widgets, the contained widgets are merely
members; I do not relegate them to a separate namespace. It's quite
simple to do most layouts writing the code by hand, without designer.
* IS-A vs. HAS-A: Your MainWindow class IS-A QMainWindow as much as it IS-A
UI_MainWindow. It cannot be instantiated without both parent classes, and
it does not make sense to consider replacing the self.ui object with
anything polymorphicly related to the Ui_MainWindow class. I think the
only reason the UI_MainWindow is a separate class is due to the
convenience of placing auto-generated code into its own file.
It's really little more than a style choice, but I think the consistency
argument will be important as you start to write more and more Qt/PySide
classes. Unless your hand-coded UIs contain a self.ui namespace, you will
confuse yourself trying to remember if a particular QObject's member functions
should access its widgets as, say, self.labelWidget or self.ui.labelWidget.
--
Nik Melchior
Sr. Roboticist | 412-379-4500 x147
Seegrid Corporation | www.seegrid.com
216 Park West Drive, Pittsburgh, PA 15275
Email Confidentiality Notice
The information contained in this transmission is confidential, proprietary or privileged and may be subject to protection under the law. This message is intended for the sole use of the individual or entity to whom it's addressed. If you are not the intended recipient, you are notified that any use, distribution or copying of the message is strictly prohibited and may subject you to criminal or civil penalties. If you received this transmission in error, please contact the sender immediately by replying to this email and delete the material from any computer.
More information about the PySide
mailing list