jeudi 1 octobre 2015

right approach setting up reusable context menus in pyqt/pyside

I am trying to implement menu in a way that I can reuse menu created for different widget however I can't think of how to get it working. I have read in book Design Patterns that

we should parameterize MenuItems with an object and not a function

how can I achieve this in python so that I can reuse some menu items at some widgets

For example I have this QMenu class:

from PyQt4.QtGui import QMenu

class MenuBase(QMenu):
    """docstring for MenuBase"""
    def __init__(self, ui):
        super(MenuBase, self).__init__()
        self._ui = ui
        self.getCommonMenus()

    @property
    def getshareMenu(self):
        self.shareMenu = QMenu('Share', self)
        self.shareMenu.addAction("Email")
        self.shareMenu.addAction("Dropbox")
        self.shareMenu.addAction("OneDrive")
        self.shareMenu.addAction("Facebook")
        return self.shareMenu

    def getCommonMenus(self):
        self.addSeparator()
        self.addAction('Show &Disk Stats', self._ui._showDiskStatSlot)
        self.addAction('&Quit', self._ui.quitAction)

    @property
    def getShareBtnMenu(self):
        return self.getshareMenu

and I want to use above menus in ui below: enter image description here

Aucun commentaire:

Enregistrer un commentaire