How can I add more than one QAction to same QMenu?

左心房为你撑大大i 提交于 2021-01-27 23:30:25

问题


I'm trying to implement Qt Menus customization, and I'm giving a feature to add same QAction more than once in the same RMB context menu. But when I try to do:

myMenu->addAction( myAction );
myMenu->addAction( myAction );

Adding QAction twice, only one instance of QAction appear on the menu. Why can't I have more than one instances of QAction? Is there any trick to achieve this? I was trying to clone/create a new action with all the propeties of previous action. But I don't know the way to get/extract a QActions's SENDER and MEMBER properties.


回答1:


This is a deliberate decision. See the documentation about it:

A QWidget should only have one of each action and adding an action it already has will not cause the same action to be in the widget twice.

The reason is probably that either there has been no use case for this, or if any, it has not covered what the majority wanted.

Based on your comment that MS Outlook allows this and you are trying to mimic it... I would personally just take the approach of warning the user when they are trying to add the same action to the same menu again.



来源:https://stackoverflow.com/questions/24847841/how-can-i-add-more-than-one-qaction-to-same-qmenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!