QAction类详解:

时间:2022-09-27 09:47:12

先贴一段描述:
Qt文档原文:

Detailed Description

The QAction class provides an abstract user interface action that can be inserted into widgets.

In applications many common commands can be invoked via menus, toolbar buttons, and keyboard shortcuts. Since the user expects each command to be performed in the same way, regardless of the user interface used, it is useful to represent each command as an action.

Actions can be added to menus and toolbars, and will automatically keep them in sync. For example, in a word processor, if the user presses a Bold toolbar button, the Bold menu item will automatically be checked.

Actions can be created as independent objects, but they may also be created during the construction of menus; the QMenu class contains convenience functions for creating actions suitable for use as menu items.

A QAction may contain an icon, menu text, a shortcut, status text, "What's This?" text, and a tooltip. Most of these can be set in the constructor. They can also be set independently with setIcon(), setText(), setIconText(), setShortcut(), setStatusTip(), setWhatsThis(), and setToolTip(). For menu items, it is possible to set an individual font with setFont().

Actions are added to widgets using QWidget::addAction() or QGraphicsWidget::addAction(). Note that an action must be added to a widget before it can be used; this is also true when the shortcut should be global (i.e., Qt::ApplicationShortcut as Qt::ShortcutContext).

Once a QAction has been created it should be added to the relevant menu and toolbar, then connected to the slot which will perform the action. For example:

openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
openAct->setShortcuts(QKeySequence::Open);
openAct->setStatusTip(tr("Open an existing file"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

fileMenu->addAction(openAct);
fileToolBar->addAction(openAct);
We recommend that actions are created as children of the window they are used in. In most cases actions will be children of the application's main window.

一、QAction类详解

QAction类提供了抽象的用户界面action,这些action可以被放置在窗口部件中。

应用程序可以通过菜单,工具栏按钮以及键盘快捷键来调用通用的命令。由于用户期望每个命令都能以相同的方式执行,而不管命令所使用的用户界面,

这个时候使用action来表示这些命令就显得十分有用。

Actions可以被添加到菜单和工具栏中,并且可以自动保持在菜单和工具栏中的同步。例如,在一个字处理软件中,如果用户在工具栏中按下了Bold按钮,那么菜单中的Bold选项就会自动被选中。

Actions可以作为独立的对象被创建,但是我们也可以在构建菜单的时候创建它们;QMenu类包含了非常简便的方法用于创建适合用作菜单项的actions。

QAction可以包括一个图标,菜单文本,快捷键,状态文本,"What`s This"文本以及一个tooltip。这些内容的绝大部分都可以在构造函数中设置。也可以通过setIcon(),setIconText(),setShortCut(),setStatusTip(),setWhatsThis和SetToolTip()函数分别设置。对于菜单项,我们还可以通过

setFont()单独为它们设置font。

可以通过QWidget::addAction()或者是QGraphicsWidget::addAction()函数将Actions添加到窗口部件上。注意,只有将Actions添加到窗口部件上之后,我们才可以使用这些actions;当actions的快捷键是全局的时候,我们也必须先将actions添加到窗口部件上。

一旦QAction被创建了,那么就必须将它添加到相关的菜单和工具栏上,然后将它们链接到实现相应action功能的槽函数上。例如:

openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);

openAct->setShortcuts(QKeySequence::Open);

openAct->setStatusTip(tr("Open an existing file"));

connect(openAct, SIGNAL(triggered()), this, SLOT(open()));

fileMenu->addAction(openAct);

fileToolBar->addAction(openAct);

我们建议将actions作为使用它们的窗口的孩子创建。在绝大多数情况下,actions都是应用程序主窗口的孩子。

类的枚举成员变量:

QAction类详解:

这个枚举类型主要是在调用QAction::activate()函数的时候被使用到。我们来看看QAction::activate()函数的原型:

QAction类详解:

从上面可以看出,我们使用该函数发射信号,而参数event则指明了发射的信号类型。基于action的widgets可以自己发射信号,然而我们也可以显式的调用本API来发射信号。

QAction类详解:

由于Mac OS X系统的一些特性,Qt 会对一些菜单项进行自动排列。比如,如果你的菜单是“关于”、“设置”、“首选项”、“退出”等等,我们可以给它们分配一个角色,Qt 则会根据这些角色对菜单项的顺序作出正确的排列。方法是,设置 QAction::menuRole 属性,例如:AboutRole、PreferencesRole、QuitRole 或者 NoRole。举例

来说,我们可以将“设置”菜单项作为 Mac OS X 的 Application::preferences。

QAction::MenuRole类型的枚举主要描述了在Mac OS X系统上,action如何移动到应用程序的菜单上。设置这个值只对菜单上的直接菜单有效,对子菜单无效。例如:如果有一个File菜单,该File菜单又包含有子菜单,那么如果你针对子菜单设置这些值,那么这些值永远不会起作用。

QAction类详解:

该优先级用于表明action在用户界面上的优先级。例如,当你的工具栏设置了Qt::ToolButtonTextBesideIcon模式,那么低优先级的actions将不会显示出标签。

【示例】

(1) 使用Action构造工具栏和菜单栏

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

{

initMenu();

initToolBar();

}

void MainWindow::initMenu()

{

/* 初始化File菜单 */

fileMenu = new QMenu(tr("File"), this);

fileOpenAction = new QAction("&Open...", this);

fileSaveAction = new QAction("&Save...", this);

fileMenu->addAction(fileOpenAction);

fileMenu->addAction(fileSaveAction);

/* 初始化Edit菜单 */

editMenu = new QMenu("&Edit");

editCopyAction = editMenu->addAction("&Copy");

editCutAction = editMenu->addAction("&Cut");

/* 将菜单添加到菜单栏上 */

QMenuBar *menuBar = this->menuBar();

menuBar->addMenu(fileMenu);

menuBar->addMenu(editMenu);

}

void MainWindow::initToolBar()

{

/* 初始化FileToolBar */

fileToolBar = new QToolBar(this);

fileToolBar->addAction(fileOpenAction);

fileToolBar->addAction(fileSaveAction);

/* 初始化EditToolBar */

editToolBar = new QToolBar(this);

editToolBar->addAction(editCopyAction);

editToolBar->addAction(editCutAction);

/* 将工具添加到工具栏上 */

addToolBar(Qt::TopToolBarArea, fileToolBar);

addToolBar(Qt::TopToolBarArea, editToolBar);

}

MainWindow::~MainWindow()

{

}

(2)测试QAction::activate(QAction::ActionEvent)

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

{

initMenu();

initToolBar();

initConnect();

}

void MainWindow::initConnect()

{

connect(fileOpenAction, SIGNAL(triggered()),

this, SLOT(sendActivate()));

connect(fileSaveAction, SIGNAL(hovered()),

this, SLOT(ansHovered()));

}

void MainWindow::sendActivate()

{

/* 这将会导致fileSaveAction发送信号QAction::hovered() */

fileSaveAction->activate(QAction::Hover);

}

void MainWindow::ansHovered()

{

qDebug("Ans!!!");

}

运行结果:

QAction类详解:

(3)测试QAction::Priority

void MainWindow::initMenu()

{

/* 初始化File菜单 */

fileMenu = new QMenu(tr("File"), this);

fileOpenAction = new QAction("&Open...", this);

fileSaveAction = new QAction("&Save...", this);

fileMenu->addAction(fileOpenAction);

fileMenu->addAction(fileSaveAction);

/* 初始化Edit菜单 */

editMenu = new QMenu("&Edit");

editCopyAction = editMenu->addAction("&Copy");

editCutAction = editMenu->addAction(QIcon(":/cut.PNG"), "&Cut");

//editCutAction->setPriority(QAction::LowPriority);

/* 将菜单添加到菜单栏上 */

QMenuBar *menuBar = this->menuBar();

menuBar->addMenu(fileMenu);

menuBar->addMenu(editMenu);

}

void MainWindow::initToolBar()

{

/* 初始化FileToolBar */

fileToolBar = new QToolBar(this);

fileToolBar->addAction(fileOpenAction);

fileToolBar->addAction(fileSaveAction);

/* 初始化EditToolBar */

editToolBar = new QToolBar(this);

editToolBar->addAction(editCopyAction);

editToolBar->addAction(editCutAction);

/* 将工具添加到工具栏上 */

addToolBar(Qt::TopToolBarArea, fileToolBar);

addToolBar(Qt::TopToolBarArea, editToolBar);

/* 设置工具栏为QT::ToolButtonTextBesideIcon */

this->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

}

此时注释掉了设置优先级的语句,图标和文字均能显示出来,效果如下图:

QAction类详解:

取消上面的注释;

/* 初始化Edit菜单 */

editMenu = new QMenu("&Edit");

editCopyAction = editMenu->addAction("&Copy");

editCutAction = editMenu->addAction(QIcon(":/cut.PNG"), "&Cut");

editCutAction->setPriority(QAction::LowPriority);

运行效果如下:

QAction类详解:

二、QActionGroup类详解

【详细描述】

QActionGroup类将actions分组。

在某些情况下将QAction分组是十分有用的。例如,如果你有一个Left Align的action,一个Right Align的action,一个Justify的action,

以及一个Center action,在统一时刻所有这些actions只能有一个被激活。一种简便的做法就是将这些actions以分组的方式组织。

下面是一个示例:

alignmentGroup = new QActionGroup(this);

alignmentGroup->addAction(leftAlignAct);

alignmentGroup->addAction(rightAlignAct);

alignmentGroup->addAction(justifyAct);

alignmentGroup->addAction(centerAct);

leftAlignAct->setChecked(true);

在上面的示例中,我们创建了一个action组。由于action group默认是互斥的,因此在同一时刻只有一个会被选中。

当组内的某个action被选中的时候,QActionGroup就会发射triggered()信号。通常情况下,组内的每个action发射自己的triggered()信号。

正如上面提到的,action group默认是互斥的;它确保在同一时刻只有一个action会被选中。如果你想创建一个action group而不想时它们是互斥关系,那么你可以通过调用setExclusive(false)来关闭互斥关系。

可以使用addAction()函数将action添加到action group中,然而更常见的做法是在创建action的时候指定一个group;这确保了这些actions具有同一个父亲。可以通过在group中添加分割线使得各个action分开显示,可以使用QAction的setSeparator()添加分割线。通常使用QWidget::addActions()函数将action group添加到窗口部件上。

【示例】

/* 初始化Action菜单 */

actionMenu = new QMenu("Action");

leftAction = new QAction("Left", this);

rightAction = new QAction("Right", this);

centerAction = new QAction("Center", this);

justifyAction = new QAction("Justify", this);

actionGroup = new QActionGroup(this);

actionMenu->addAction(actionGroup->addAction(leftAction));

actionMenu->addAction(actionGroup->addAction(rightAction));

actionMenu->addAction(actionGroup->addAction(centerAction));

actionMenu->addAction(actionGroup->addAction(justifyAction));

可以使用下面的槽函数验证默认情况下,一次只有一个action可以被选中。当然也可以设置action的

bool

isExclusive () const

为false,使得一次可以选中多个:

void MainWindow::initConnect()

{

connect(fileOpenAction, SIGNAL(triggered()),

this, SLOT(sendActivate()));

connect(fileSaveAction, SIGNAL(hovered()),

this, SLOT(ansHovered()));

connect(leftAction, SIGNAL(triggered()),

this, SLOT(ansTriggered()));

connect(rightAction, SIGNAL(triggered()),

this, SLOT(ansTriggered()));

connect(centerAction, SIGNAL(triggered()),

this, SLOT(ansTriggered()));

connect(justifyAction, SIGNAL(triggered()),

this, SLOT(ansTriggered()));

}

运行效果:

QAction类详解:

备注:

1、放置到ActionGroup中就默认是exclusive。

2、是否出现选中的标志"小圆点"是通过设置setcheckable完成的。两者并无联系。

关于QActionGroup的使用,我发的一个帖子中有提到,再次感谢网友jdwx1

帖子的连接如下,感兴趣的可以看看QActionGroup只对子菜单生效?

三、QWidgetAction类详解

【详细描述】

QWidgetAction通过接口方式继承自QAction,用于将自定义的widget插入基于action的容器,例如工具栏。

出现在应用程序中的绝大多数的actions都是代表了一个菜单项或工具栏中的一个按钮。然而有时候我们也许要复杂一点的widgets。例如,字处理程序工具栏中使用QComboBox实现zoom action,实现不同程度的缩放。QToolBar提供了QToolBar::insertWidget()函数可以十分方便的将单个widget插入到合适的位置。然而,如果你想在多个容器中实现自定义widget的action,那么你就必须实现QWidgetAction的子类。

如果QWidgetAction添加到QToolBar,那么就会调用QWidgetAction::createWidget()。我们可以重新实现这个函数创建自定义的widget。

如果将一个action从容器widget上删除,那么就会调用QWidgetAction::deleteWidget(),调用该函数的参数就是上面创建的自定义widget。该函数的默认实现是将widget隐藏,然后使用QObject::deleteLater()删除它。

如果你只有一个自定义的widget,那么你就可以使用setDefaultWidget()函数将它设置为默认的widget。那么以后当action被添加到QToolBar上时,就会自动将该自定义的widget添加到QToolBar上。如果将仅有一个默认widget的QWidgetAction同时添加到两个工具栏上,那么仅有第一个添加才会显示出来。QWidgetAction接管了默认的widget。

注意:这取决于widget激活action,例如重新实现鼠标事件处理者,然后调用QAction::trigger()。

Mac OS X:在Mac OS X上,如果你将一个widget添加到应用程序菜单栏的某个菜单上,那么该widget可以显示出来,并且可以实现功能,只是有一些限制:

1、该widget的父对象不再是QMenu而是原生的菜单视图。如果你在其他地方显示菜单(例如作为一个弹出菜单),那么该菜单不会显 示在你期望的地方;

2、针对该widget的Focus/Keyboard处理不再可用;

3、由于Apple的设计,该widget的鼠标轨迹暂时不可用;

4、将triggered()信号链接到打开模态对话框的槽函数上会导致应用程序崩溃(在Mac OS X10.4,这被告知是Apple的一个BUG),一个规避的方法是使用QueuedConnection代替DirecConnection。

【示例】

本示例代码来自:http://www.qtcn.org/bbs/simple/?t28610.html

功能:设置QMenu中菜单项的高度

代码片段:

class MyMenuItem:public QWidget

{

Q_OBJECT

public:

MyMenuItem(QWidget *parent)

{

new QLabel("test",this);

}

};

int main(int argc, char *argv[])

{

popupMenu = new QMenu(this);

QAction *action1 = new QAction(tr("&New1"), this);

QAction *action2 = new QAction(tr("&New2"), this);

QAction *action3 = new QAction(tr("&New3"), this);

QAction *action4 = new QAction(QIcon("./load.png"), tr("Bookstore"), this);

popupMenu->addAction(action1);

popupMenu->addAction(action2);

popupMenu->addAction(action3);

popupMenu->addAction(action4);

MyMenuItem *item1 = new MyMenuItem(this);

item1->setFixedSize(100,100); //这里可以设置大小

QWidgetAction *action1 = new QWidgetAction(popupMenu);

action1->setDefaultWidget(item1);

MyMenuItem *item2 = new MyMenuItem(this);

QWidgetAction *action2 = new QWidgetAction(popupMenu);

action2->setDefaultWidget(item2);

MyMenuItem *item3 = new MyMenuItem(this);

QWidgetAction *action3 = new QWidgetAction(popupMenu);

action3->setDefaultWidget(item3);

popupMenu->exec();

转自:http://qiusuoge.com/12287.html

QAction类详解:的更多相关文章

  1. QAction系列详解

    QAction系列详解 一.QAction类详解 [详细描述] QAction类提供了抽象的用户界面action,这些action可以被放置在窗口部件中.        应用程序可以通过菜单,工具栏按 ...

  2. java之StringBuffer类详解

    StringBuffer 线程安全的可变字符序列. StringBuffer源码分析(JDK1.6): public final class StringBuffer extends Abstract ...

  3. java之AbstractStringBuilder类详解

    目录 AbstractStringBuilder类 字段 构造器 方法   public abstract String toString() 扩充容量 void  expandCapacity(in ...

  4. java之StringBuilder类详解

    StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...

  5. Java String类详解

    Java String类详解 Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时候,我们对它既熟悉又陌生. 类结构: public final ...

  6. JAVAEE学习——struts2_01:简介、搭建、架构、配置、action类详解和练习:客户列表

    一.struts2是什么 1.概念 2.struts2使用优势以及历史 二.搭建struts2框架 1.导包 (解压缩)struts2-blank.war就会看到 2.书写Action类 public ...

  7. Struts2-整理笔记(二)常量配置、动态方法调用、Action类详解

    1.修改struts2常量配置(3种) 第一种 在str/struts.xml中添加constant标签 <struts> <!-- 如果使用使用动态方法调用和include冲突 - ...

  8. C&num; 内置 DateTime类详解

    C# 内置 DateTime类详解 摘抄自微软官方文档,用来方便自己查阅:网址:https://msdn.microsoft.com/zh-cn/library/system.datetime(v=v ...

  9. Android游戏开发之旅 View类详解

    Android游戏开发之旅 View类详解 自定义 View的常用方法: onFinishInflate() 当View中所有的子控件 均被映射成xml后触发 onMeasure(int, int) ...

随机推荐

  1. GNS3 桥接虚拟网卡 telnet 实验

    网上很多桥接本地网卡的,一直测试不通.无奈,本人桥接vmware 虚拟网卡通! 1: 2: 3:telnet 加密实验 R1(config)#line vt R1(config)#line vty 0 ...

  2. JS魔法堂:ASI&lpar;自动分号插入机制&rpar;和前置分号

    一.前言 今晚在知乎看到百姓网前端技术专家——贺师俊对<JavaScript 语句后应该加分号么?>的回答,让我又一次看到大牛的风采,实在佩服万分.但单纯的敬佩是不足以回报他如此优秀的文字 ...

  3. git生成秘钥之后同步到服务器

    现在本地生成ssh私钥和公钥 设置本地git用户配置 $ git config --global user.name "username"$ git config --global ...

  4. Oracle中not exists 与not in 的使用情况

    1.在oracle11g以上版本,oracle已经做了优化,能够自动将in优化成exists方式,因此oracle11g以上版本,使用in和exists效果是一样的. 2.在oracle中,使用not ...

  5. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. qt 共享内存 单例

        QT 进程间通信之古老的方法(内存共享)     让QT只运行一个实例     以上两篇文章中分别讲述了QSharedMemory的不同作用,第一篇讲了进程间通信,第二篇讲述了怎么让应用程序只 ...

  7. 聪聪和可可 HYSBZ - 1415(概率 &plus; spfa &plus; 记忆化dp)

    Input 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行,每 ...

  8. 命令查看linux主机配置

    查看cpu: # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cp ...

  9. 《转》python学习(6)序列类型-字符串

    转自 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125502.html 二.序列类型 包含字符串.列表.元祖.模式都一样,举一反三即可.如 ...

  10. LeetCode赛题390----Elimination Game

    # 390. Elimination Game There is a list of sorted integers from 1 to n. Starting from left to right, ...