qt 工具栏只能一个按钮按下 QActionGroup

Qt在线文档地址:

https://doc.qt.io/qt-5/qactiongroup.html

此为官方文档的摘抄,笔者无版权。

使用QActionGroup就会使得加入QActionGroup的QAction只有一个被按下,同样的用法的还有QButtonGroup

需要注意的是你必须增加setCheckable(true);不然按钮按下就弹起来因此文档中还需增加以下代码:

leftAlignAct->setCheckable(true);
rightAlignAct->setCheckable(true);
justifyAct->setCheckable(true);
centerAct->setCheckable(true);

QActionGroup一个行动组默认是排他性的; 它确保在任何时候只有一个可检查操作处于活动状态。 如果您想将可检查的操作分组而不排除它们,您可以通过调用setExclusive(false)来打开排他性,这你就可以多个工具同时按下。

Detailed Description

In some situations it is useful to group QAction objects together. For example, if you have a Left Align action, a Right Align action, a Justifyaction, and a Center action, only one of these actions should be active at any one time. One simple way of achieving this is to group the actions together in an action group.

Here's a example (from the Menus example):

    alignmentGroup = new QActionGroup(this);
    alignmentGroup->addAction(leftAlignAct);
    alignmentGroup->addAction(rightAlignAct);
    alignmentGroup->addAction(justifyAct);
    alignmentGroup->addAction(centerAct);
    leftAlignAct->setChecked(true);

Here we create a new action group. Since the action group is exclusive by default, only one of the actions in the group is checked at any one time.

Alignment options in a QMenu

 

A QActionGroup emits an triggered() signal when one of its actions is chosen. Each action in an action group emits its triggered() signal as usual.

As stated above, an action group is exclusive by default; it ensures that at most only one checkable action is active at any one time. If you want to group checkable actions without making them exclusive, you can turn off exclusiveness by calling setExclusive(false).

By default the active action of an exclusive group cannot be unchecked. In some cases it may be useful to allow unchecking all the actions, you can allow this by calling setExclusionPolicy(QActionGroup::ExclusionPolicy::ExclusiveOptional).

Actions can be added to an action group using addAction(), but it is usually more convenient to specify a group when creating actions; this ensures that actions are automatically created with a parent. Actions can be visually separated from each other by adding a separator action to the group; create an action and use QAction's setSeparator() function to make it considered a separator. Action groups are added to widgets with the QWidget::addActions() function.

See also QAction.

 

 

这里有比别人翻译的一篇博文,可以参考下:

https://blog.csdn.net/explorer_day/article/details/80359065

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt工具栏中创建下拉菜单可以通过以下步骤实现: 1. 首先,在 Qt 设计器中打开你的窗口/对话框界面,将工具栏添加到界面上。 2. 在工具栏上右键单击,选择 "Add Action Group",创建一个动作组。 3. 在动作组中添加一个动作,该动作将作为下拉菜单的标题显示。 4. 在动作组中添加子动作,这些子动作将作为下拉菜单的选项。 5. 右键单击动作组中的子动作,选择 "Set Menu",然后选择对应的菜单。 6. 在代码中,连接菜单的信号和槽函数,以便在选择菜单项时执行相应的操作。 下面是一个简单的示例代码,演示如何在工具栏上创建一个下拉菜单: ```cpp // 创建工具栏 QToolBar* toolbar = new QToolBar(this); addToolBar(toolbar); // 创建动作组 QActionGroup* actionGroup = new QActionGroup(this); // 创建主动作并设置为不可点击 QAction* mainAction = new QAction("下拉菜单", this); mainAction->setCheckable(true); mainAction->setEnabled(false); toolbar->addAction(mainAction); // 创建下拉菜单 QMenu* menu = new QMenu(this); mainAction->setMenu(menu); // 创建子动作并添加到下拉菜单 QAction* action1 = new QAction("选项1", this); QAction* action2 = new QAction("选项2", this); menu->addAction(action1); menu->addAction(action2); // 将子动作添加到动作组 actionGroup->addAction(action1); actionGroup->addAction(action2); // 连接菜单的信号和槽函数 connect(actionGroup, SIGNAL(triggered(QAction*)), this, SLOT(menuActionTriggered(QAction*))); // 槽函数的实现 void YourClass::menuActionTriggered(QAction* action) { if (action == action1) { // 处理选项1的操作 } else if (action == action2) { // 处理选项2的操作 } } ``` 通过以上步骤,你可以在 Qt工具栏上成功创建一个下拉菜单,并在选择菜单项时执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值