QAction类详解:

QAction是Qt库中提供的一种抽象用户界面行动,适用于菜单、工具栏和快捷键。它允许开发者创建可同步更新的命令,如在菜单和工具栏之间。QAction可以设置图标、文本、快捷键等属性,并通过信号槽机制与功能实现连接。在创建后,QAction需添加到相应窗口部件才能使用,推荐作为窗口的子对象创建。
摘要由CSDN通过智能技术生成

先贴一段描述:
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;当act

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值