allow to create QActionGroups from the menu object, returned Group automates the "adding to the group" step
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
from PyQt5.QtWidgets import QAction
|
||||
from PyQt5.QtWidgets import QAction, QActionGroup
|
||||
|
||||
|
||||
class MenuBase:
|
||||
@ -29,5 +29,33 @@ class MenuBase:
|
||||
def addSeparator(self):
|
||||
self.qmenu.addSeparator()
|
||||
|
||||
def addGroup(self):
|
||||
group = Group(self)
|
||||
return group
|
||||
|
||||
|
||||
|
||||
class Group:
|
||||
|
||||
def __init__(self, menu):
|
||||
self.menu = menu
|
||||
self.qactiongroup = QActionGroup(menu.qmenu)
|
||||
|
||||
|
||||
def __getattr__(self, name):
|
||||
attr = getattr(self.menu, name)
|
||||
|
||||
if not callable(attr):
|
||||
return attr
|
||||
|
||||
# replace attr by a wrapper that automatically adds returned actions to the group
|
||||
def wrapper(*args, **kwargs):
|
||||
res = attr(*args, **kwargs)
|
||||
if isinstance(res, QAction):
|
||||
self.qactiongroup.addAction(res)
|
||||
return res
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user