python - How to refactor those basic classes (PyQt5) -



python - How to refactor those basic classes (PyQt5) -

i learning pyqt5 , tried create extended widgets. learning oop lack experience project.

my final goal have master widgets can disable/enable slave widgets. far need check , radio buttons.

so tried create abstract class contains extended behavior of widgets (the management of state of slaves widgets):

class qdisablingwidget(): __metaclass__ = abcmeta def __init__(self): self.slavewidgets = [] self.slavestatewhenmasterisenabled = {} def addslavewidget(self, slavewidget, isenabledwhenmasterisenabled=true): [...] def updateslavestatus(self): [...]

then create extended widget classes:

class qdisablingcheckbox(qtwidgets.qcheckbox, qdisablingwidget): def __init__(self, text=none, parent=none, isinmutexgroup=false): super(qtwidgets.qcheckbox, self).__init__() super(qdisablingwidget, self).__init__() if text: self.settext(text) if parent: self.setparent(parent) self.isinmutexgroup = isinmutexgroup # click signal handling self.statechanged.connect(self.updateslavestatus) class qdisablingradiobutton(qtwidgets.qradiobutton, qdisablingwidget): def __init__(self, text=none, parent=none, isinmutexgroup=false): super(qtwidgets.qradiobutton, self).__init__() super(qdisablingwidget, self).__init__() if text: self.settext(text) if parent: self.setparent(parent) self.isinmutexgroup = isinmutexgroup # click signal handling self.toggled.connect(self.updateslavestatus)

you can see problem: need connect self.updateslavestatus right signals (statechanged , toggled) added in constructor of derived classes. added isinmutexgroup argument implementation reasons , realize duplicating code in both derived classes...

it first time seek utilize oop "for real" (first effort of multiple inheritance , abstract class), if know breaking beauty of oop concept, don't know nice class hierarchy...

so basically, looking solution on example. looking guidelines, general advice, tutorials, etc. help me!

thank help.

even if got downvote question, think beginners interested solution found:

i have extension abstract class that:

class qdisablingwidget(qtcore.qobject): __metaclass__ = abcmeta def __init__(self, isinmutexgroup=false, **kwds): [...]

then can derive class that:

class qdisablingcheckbox(qtwidgets.qcheckbox, qdisablingwidget): def __init__(self, **kwds): super().__init__(**kwds) # on click signal handling self.statechanged.connect(self.updateslavestatus)

and

class qdisablingradiobutton(qtwidgets.qradiobutton, qdisablingwidget): def __init__(self, **kwds): super().__init__(**kwds) # on click signal handling self.toggled.connect(self.updateslavestatus)

finally, when utilize classes need create object that:

disablingradiobut = qwidgets.qdisablingradiobutton(text="my button", parent=self, isinmutexgroup=true)

i.e must utilize keywords explicitly each constructors eat kewords use/know. approach have maximum reusability of extension class.

i got solution here: http://pyqt.sourceforge.net/docs/pyqt5/multiinheritance.html

and more details here: http://rhettinger.wordpress.com/2011/05/26/super-considered-super/

a nice article!

python oop pyqt refactoring

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -