Skip to content

Commit

Permalink
fix: MMenu multi select no a correct action
Browse files Browse the repository at this point in the history
  • Loading branch information
ZSD-tim authored and muyr committed Jun 12, 2024
1 parent cd301a7 commit 53010a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions dayu_widgets/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,15 +566,16 @@ def set_value(self, data):
def _set_value(self, value):
data_list = value if isinstance(value, list) else [value]
flag = False
selected = "/".join(data_list)
for act in self._action_group.actions():
checked = act.property("value") in data_list
checked = act.property("long_path") == selected
if act.isChecked() != checked: # 更新来自代码
act.setChecked(checked)
flag = True
if flag:
self.sig_value_changed.emit(value)

def _add_menu(self, parent_menu, data_dict):
def _add_menu(self, parent_menu, data_dict, long_path=""):
if "children" in data_dict:
menu = MMenu(title=data_dict.get("label"), parent=self)
menu.setProperty("value", data_dict.get("value"))
Expand All @@ -583,14 +584,18 @@ def _add_menu(self, parent_menu, data_dict):
# 用来将来获取父层级数据
menu.setProperty("parent_menu", parent_menu)
for i in data_dict.get("children"):
self._add_menu(menu, i)
long_path = "{root}/{label}".format(
root=long_path or data_dict.get("label"), label=i.get("label")
)
self._add_menu(menu, i, long_path=long_path)
else:
action = self._action_group.addAction(
utils.display_formatter(data_dict.get("label"))
)
action.setProperty("value", data_dict.get("value"))
action.setCheckable(True)
# 用来将来获取父层级数据
action.setProperty("long_path", long_path)
action.setProperty("parent_menu", parent_menu)
parent_menu.addAction(action)

Expand Down
8 changes: 8 additions & 0 deletions examples/combo_box_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ def dynamic_get_city():
"value": "\u5317\u4eac",
"label": "\u5317\u4eac",
},
{
"children": [
{"value": "\u6545\u5bab", "label": "\u6545\u5bab"},
],
"value": "\u4e1c\u4eac",
"label": "\u4e1c\u4eac",
},
{
"children": [
{
Expand Down Expand Up @@ -122,6 +129,7 @@ def dynamic_get_city():
select4.set_menu(menu4)
select4.set_formatter(lambda x: " / ".join(x))
self.bind("button4_selected", select4, "value", signal="sig_value_changed")
select4.set_value("北京/故宫")

self.register_field("button5_selected", "")
menu5 = MMenu(exclusive=False, parent=self)
Expand Down

0 comments on commit 53010a5

Please sign in to comment.