当前位置: 动力学知识库 > 问答 > 编程问答 >

python - How to refactor those basic classes (PyQt5)

问题描述:

I am learning PyQt5 and I tried to create a extended widgets.

I am also learning OOP so I lack experience for this project.

My final goal is to have master widgets that can disable/enable some slave widgets.

So far I need check and radio buttons.

So I tried to create an abstract class that contains the extended behavior of the widgets (the management of the state of the slaves widgets):

class QDisablingWidget():

__metaclass__ = ABCMeta

def __init__(self):

self.slaveWidgets = []

self.slaveStateWhenMasterIsEnabled = {}

def addSlaveWidget(self, slaveWidget, isEnabledWhenMasterIsEnabled=True):

[...]

def updateSlaveStatus(self):

[...]

Then I create my 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 already see the problem:

I need to connect my self.updateSlaveStatus to the correct signals (stateChanged and toggled) so I added it in the constructor of the derived classes.

Recently I also added the isInMutexGroup argument for some implementation reasons and I realize that I am duplicating the code in both derived classes...

It is the first time I try to use OOP "for real" (first attempt of multiple inheritance and abstract class), so even if I know I am breaking the beauty of the OOP concept, I don't know what to do to get a nice class hierarchy...

So basically, I am looking for a solution on this example. But I am also looking for guidelines, general advice, tutorials, etc. Actually anything that could help me!

Thank you for your help.

网友答案:

Even if I got a downvote for that question, I think some beginners could be interested by the solution I found:

I have my extension abstract class like that:

class QDisablingWidget(QtCore.QObject):
    __metaclass__ = ABCMeta
    def __init__(self, isInMutexGroup=False, **kwds):
        [...]

Then I can derive class like 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 I use the classes I need to create object like that:

disablingRadioBut = QWidgets.QDisablingRadioButton(text="My button",
                                                   parent=self,
                                                   isInMutexGroup=True)

I.e I must use keywords explicitly so that each constructors will eat the kewords the use/know. Thanks to this approach I have maximum reusability of my extension class.

I got this 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 very nice article!

分享给朋友:
您可能感兴趣的文章:
随机阅读: