python - Sharing code between two almost identical widgets PySide -


I have two widgets that are almost identical, the difference is one of them is an extra button, both of them have a bunch of methods. Which are exactly the same. Is it the code to share between them? The best option here is subclassing? Some day I would like to change the superclass with the procedure in the sub-class.

Just use regular legacy.

  class A (QtGui.QWidget): def __init __ (self): super () .__ init __ () self.x = 1 self._initProperties () # values ​​in legacy Special method for change # End manufacturer def _initProperties (self): "" ". Special inheritance properties start" "" self.setLayout (QtGui.QVBoxLayout ()) # end_initProperties def do_something (self): return self.x class B (A): # def __init __ (self): # super () .__ init (__) # # self.y = 2 # # Because we have _initProperties that will be at the proper time we really do not need # __init__ Is just using _initProperties A. # However, I will still use __init__ I just comment it out as an example. DEF _initProperties (self): "Start" special inheritance values ​​attribute: ... we have not done super phone, so we are not using parents _initProperties methods we are overriding parents method "" "self .y = 2 self.setLayout (QtGui.QHBoxLayout ()) # end_initProperties def do_something (self):. Return super () do_something () + self.y  

The alternative option is to create a regular object class mixin. MyMixin (object): def __init __ (self): super MyMixin, QtGui.QWidget): Close class B (MyMixin, QtGui.QGroupBox): def __init __ (self) super () .__ init__ () self.y = 2 def do_something (self): return super () do_something ( ) + Self .. Y

Python supports multiple inheritance, this approach can be a QWidget with class A, while square b is some QGroupBox


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

javascript - amcharts makechart not working -

java - Algorithm negotiation fail SSH in Jenkins -