admin管理员组文章数量:1406924
In the following code, I connect the triggered signals of QMenu.menuAction and QAction to similar slot functions. However, only the triggered signal of QAction is triggered. I am curious why this is?
menu = self.menuBar().addMenu("action_1")
menu.menuAction().triggered.connect(lambda: print("action_1"))
action = self.menuBar().addAction("action_2")
action.triggered.connect(lambda: print("action_2"))
In the following code, I connect the triggered signals of QMenu.menuAction and QAction to similar slot functions. However, only the triggered signal of QAction is triggered. I am curious why this is?
menu = self.menuBar().addMenu("action_1")
menu.menuAction().triggered.connect(lambda: print("action_1"))
action = self.menuBar().addAction("action_2")
action.triggered.connect(lambda: print("action_2"))
Share
Improve this question
asked Mar 6 at 12:14
poisonpoison
254 bronze badges
4
|
1 Answer
Reset to default 2The main purpose of the action returned from a QMenu menuAction()
is to add that action to another "action container".
The common cases are:
- a parent QMenu having sub menus;
- a QMenuBar with multiple menus, each one shown as the title of those menu;
- a QToolButton using
setDefaultAction()
, which will automatically show the menu related to that action, similarly (but not like) usingsetMenu()
;
Leaving out the third case, which is a bit of an exception, the first two cases are "action containers" that show a list of actions.
Conceptually speaking, QMenuBar is similar to a persistent QMenu (see the documentation about using "tear-off" menus), for which some actions actually contain a sub menu: the menuAction()
is what is shown in the parent menu (or the menu bar).
Now, it's easy to get confused by the identical names of the triggered
signals available for QMenu and QAction. There is an important difference, though:
QMenu.triggered
: "This signal is emitted when an action in this menu is triggered"; QMenuBar has a similar signal as well that behaves identically;QAction.triggered
: "This signal is emitted whenan[the] action is activated by the user";
Considering all the above, the result is that:
- when triggering an action in a menu (by clicking it, by pressing Enter when selected, or by explicitly calling
trigger()
), only that action will emit thetriggered
signal; - whenever an action in the current menu or menu bar (including actions in a sub menu) is triggered, the QMenu or QMenuBar will emit its
triggered
signal with the action as its argument;
When you click the action
named "action_2", you are only triggering that action, not the action of its parent menu. That's why you don't get the triggered
signal from the menuAction()
: the action associated with that menu has not being triggered (nor it should!).
The triggered
signal of the menu action of a QMenu is normally never used, and for obvious reasons: when the user clicks an item in a parent menu that refers to a sub menu (or a menu title in a menu bar) they usually want to just open that menu.
Finally, there are cases for which the triggered
signal of a menu action is used, and they're mostly used for advanced purposes: for instance, by creating a QMenu subclass that implements special calls (eg: when double clicking on a menu action of a submenu); those are very rare cases, though, normally implemented for special cases, done with high awareness of the Qt toolkit behavior and extended experience with it. Also, in those cases the triggered
signal has to be explicitly emitted, because Qt doesn't provide ways to automatically emit that, based on the assumption that there's no direct way to trigger the action related to a menu.
本文标签: qtThe difference between QMenumenuAction and QAction in signal processingStack Overflow
版权声明:本文标题:qt - The difference between QMenu.menuAction and QAction in signal processing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744977067a2635582.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
QMenu
does not represent an action, so itsmenuAction
wll never be triggered. What are you actually trying to achieve? – ekhumoro Commented Mar 7 at 12:52QMenuBar
uses theaddMenu(title: str)
method to add a submenu, it first constructs aQMenu
, and then callsaddAction
to addQMenu.menuAction
toQMenuBar
. So when I tried to use thetriggered
signal of theAction
returned bymenuAction
, I found this difference. It aroused my curiosity. But my C++ level is very limited, and it is a challenge for me to continue reading the source code. So I asked this question. @ekhumoro – poison Commented Mar 8 at 3:15