简体   繁体   English

Qt从QMenu删除动作以稍后再添加

[英]Qt remove action from QMenu to add it again later

I need to remove actions from a QMenu , but I need to add them later. 我需要从QMenu删除操作,但稍后需要添加它们。

The problem is that whan I re-add them, they dont appear (probably because the actions are deleted when I remove them from the Menu). 问题是,当我重新添加它们时,它们没有出现(可能是因为当我从菜单中将其删除时,这些动作已被删除)。

How can I do this? 我怎样才能做到这一点?

Note that hiding/disabling, etc is not suitable for me, I really need to remove them 请注意,隐藏/禁用等内容不适合我,我真的需要删除它们

You don't say precisely how you create/add/remove the actions from the QMenu so I can't comment on what you're currently doing, but... you should be able to create/manage some of the actions yourself and then use the QWidget::addAction(QAction *) overload -- it doesn't assume ownership of the QAction passed as a parameter. 您没有确切地说出如何从QMenu创建/添加/删除操作,因此我无法评论您当前正在做什么,但是...您应该能够自己创建/管理一些操作,并然后使用QWidget::addAction(QAction *)重载-它假定作为参数传递的QAction所有权。

QMenu menu;
QAction action_I_Want_to_manage("Save...");
menu.addAction("File...");
menu.addAction(&action_I_Want_to_manage);
menu.exec(QCursor::pos());

/*
 * Remove the action temporarily...
 */
menu.removeAction(&action_I_Want_to_manage);
menu.exec(QCursor::pos());

/*
 * ...stick it back in.
 */
menu.addAction(&action_I_Want_to_manage);
menu.exec(QCursor::pos());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM