Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QAction Class

TheQAction class provides an abstract user interface action that can be inserted into widgets.More...

Header:#include <QAction>
Inherits:QObject
Inherited By:

QMenuItem andQWidgetAction

Public Types

enumActionEvent { Trigger, Hover }
enumMenuRole { NoRole, TextHeuristicRole, ApplicationSpecificRole, AboutQtRole, ..., QuitRole }
enumPriority { LowPriority, NormalPriority, HighPriority }
enumSoftKeyRole { NoSoftKey, PositiveSoftKey, NegativeSoftKey, SelectSoftKey }

Properties

Public Functions

QAction(QObject * parent)
QAction(const QString & text, QObject * parent)
QAction(const QIcon & icon, const QString & text, QObject * parent)
~QAction()
QActionGroup *actionGroup() const
voidactivate(ActionEvent event)
QList<QGraphicsWidget *>associatedGraphicsWidgets() const
QList<QWidget *>associatedWidgets() const
boolautoRepeat() const
QVariantdata() const
QFontfont() const
QIconicon() const
QStringiconText() const
boolisCheckable() const
boolisChecked() const
boolisEnabled() const
boolisIconVisibleInMenu() const
boolisSeparator() const
boolisVisible() const
QMenu *menu() const
MenuRolemenuRole() const
QWidget *parentWidget() const
Prioritypriority() const
voidsetActionGroup(QActionGroup * group)
voidsetAutoRepeat(bool)
voidsetCheckable(bool)
voidsetData(const QVariant & userData)
voidsetFont(const QFont & font)
voidsetIcon(const QIcon & icon)
voidsetIconText(const QString & text)
voidsetIconVisibleInMenu(bool visible)
voidsetMenu(QMenu * menu)
voidsetMenuRole(MenuRole menuRole)
voidsetPriority(Priority priority)
voidsetSeparator(bool b)
voidsetShortcut(const QKeySequence & shortcut)
voidsetShortcutContext(Qt::ShortcutContext context)
voidsetShortcuts(const QList<QKeySequence> & shortcuts)
voidsetShortcuts(QKeySequence::StandardKey key)
voidsetSoftKeyRole(SoftKeyRole softKeyRole)
voidsetStatusTip(const QString & statusTip)
voidsetText(const QString & text)
voidsetToolTip(const QString & tip)
voidsetWhatsThis(const QString & what)
QKeySequenceshortcut() const
Qt::ShortcutContextshortcutContext() const
QList<QKeySequence>shortcuts() const
boolshowStatusText(QWidget * widget = 0)
SoftKeyRolesoftKeyRole() const
QStringstatusTip() const
QStringtext() const
QStringtoolTip() const
QStringwhatsThis() const
  • 29 public functions inherited fromQObject

Public Slots

voidhover()
voidsetChecked(bool)
voidsetDisabled(bool b)
voidsetEnabled(bool)
voidsetVisible(bool)
voidtoggle()
voidtrigger()
  • 1 public slot inherited fromQObject

Signals

voidchanged()
voidhovered()
voidtoggled(bool checked)
voidtriggered(bool checked = false)

Reimplemented Protected Functions

virtual boolevent(QEvent * e)
  • 8 protected functions inherited fromQObject

Additional Inherited Members

  • 7 static public members inherited fromQObject
  • 8 protected functions inherited fromQObject

Detailed Description

TheQAction 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 anaction.

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; theQMenu class contains convenience functions for creating actions suitable for use as menu items.

AQAction 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 withsetIcon(),setText(),setIconText(),setShortcut(),setStatusTip(),setWhatsThis(), andsetToolTip(). For menu items, it is possible to set an individual font withsetFont().

Actions are added to widgets usingQWidget::addAction() orQGraphicsWidget::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 asQt::ShortcutContext).

Once aQAction 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=newQAction(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.

See alsoQMenu,QToolBar, andApplication Example.

Member Type Documentation

enum QAction::ActionEvent

This enum type is used when callingQAction::activate()

ConstantValueDescription
QAction::Trigger0this will cause theQAction::triggered() signal to be emitted.
QAction::Hover1this will cause theQAction::hovered() signal to be emitted.

enum QAction::MenuRole

This enum describes how an action should be moved into the application menu on Mac OS X.

ConstantValueDescription
QAction::NoRole0This action should not be put into the application menu
QAction::TextHeuristicRole1This action should be put in the application menu based on the action's text as described in theQMenuBar documentation.
QAction::ApplicationSpecificRole2This action should be put in the application menu with an application specific role
QAction::AboutQtRole3This action matches handles the "About Qt" menu item.
QAction::AboutRole4This action should be placed where the "About" menu item is in the application menu. The text of the menu item will be set to "About <application name>". The application name is fetched from theInfo.plist file in the application's bundle (SeeDeploying an Application on Mac OS X).
QAction::PreferencesRole5This action should be placed where the "Preferences..." menu item is in the application menu.
QAction::QuitRole6This action should be placed where the Quit menu item is in the application menu.

Setting this value only has effect on items that are in the immediate menus of the menubar, not the submenus of those menus. For example, if you have File menu in your menubar and the File menu has a submenu, setting the MenuRole for the actions in that submenu have no effect. They will never be moved.

enum QAction::Priority

This enum defines priorities for actions in user interface.

ConstantValueDescription
QAction::LowPriority0The action should not be prioritized in the user interface.
QAction::NormalPriority128 
QAction::HighPriority256The action should be prioritized in the user interface.

This enum was introduced or modified in Qt 4.6.

See alsopriority.

enum QAction::SoftKeyRole

This enum describes how an action should be placed in the softkey bar. Currently this enum only has an effect on the Symbian platform.

ConstantValueDescription
QAction::NoSoftKey0This action should not be used as a softkey
QAction::PositiveSoftKey1This action is used to describe a softkey with a positive or non-destructive role such as Ok, Select, or Options.
QAction::NegativeSoftKey2This action is used to describe a soft ey with a negative or destructive role role such as Cancel, Discard, or Close.
QAction::SelectSoftKey3This action is used to describe a role that selects a particular item or widget in the application.

Actions with a softkey role defined are only visible in the softkey bar when the widget containing the action has focus. If no widget currently has focus, the softkey framework will traverse up the widget parent hierarchy looking for a widget containing softkey actions.

This enum was introduced or modified in Qt 4.6.

Property Documentation

autoRepeat :bool

This property holds whether the action can auto repeat.

If true, the action will auto repeat when the keyboard shortcut combination is held down, provided that keyboard auto repeat is enabled on the system. The default value is true.

This property was introduced in Qt 4.2.

Access functions:

boolautoRepeat() const
voidsetAutoRepeat(bool)

Notifier signal:

voidchanged()

checkable :bool

This property holds whether the action is a checkable action.

A checkable action is one which has an on/off state. For example, in a word processor, a Bold toolbar button may be either on or off. An action which is not a toggle action is a command action; a command action is simply executed, e.g. file save. By default, this property is false.

In some situations, the state of one toggle action should depend on the state of others. For example, "Left Align", "Center" and "Right Align" toggle actions are mutually exclusive. To achieve exclusive toggling, add the relevant toggle actions to aQActionGroup with theQActionGroup::exclusive property set to true.

Access functions:

boolisCheckable() const
voidsetCheckable(bool)

Notifier signal:

voidchanged()

See alsoQAction::setChecked().

checked :bool

This property holds whether the action is checked.

Only checkable actions can be checked. By default, this is false (the action is unchecked).

Access functions:

boolisChecked() const
voidsetChecked(bool)

Notifier signal:

voidtoggled(bool checked)

See alsocheckable.

enabled :bool

This property holds whether the action is enabled.

Disabled actions cannot be chosen by the user. They do not disappear from menus or toolbars, but they are displayed in a way which indicates that they are unavailable. For example, they might be displayed using only shades of gray.

What's This? help on disabled actions is still available, provided that theQAction::whatsThis property is set.

An action will be disabled when all widgets to which it is added (withQWidget::addAction()) are disabled or not visible. When an action is disabled, it is not possible to trigger it through its shortcut.

By default, this property is true (actions are enabled).

Access functions:

boolisEnabled() const
voidsetEnabled(bool)

Notifier signal:

voidchanged()

See alsotext.

font :QFont

This property holds the action's font.

The font property is used to render the text set on theQAction. The font will can be considered a hint as it will not be consulted in all cases based upon application and style.

By default, this property contains the application's default font.

Access functions:

QFontfont() const
voidsetFont(const QFont & font)

Notifier signal:

voidchanged()

See alsoQAction::setText() andQStyle.

icon :QIcon

This property holds the action's icon.

In toolbars, the icon is used as the tool button icon; in menus, it is displayed to the left of the menu text. There is no default icon.

On Symbian the icons which are passed to softkeys, i.e. to actions with softkey role, need to have pixmap alpha channel correctly set otherwise drawing artifacts will appear when softkey is pressed down.

If a null icon (QIcon::isNull() is passed into this function, the icon of the action is cleared.

Access functions:

QIconicon() const
voidsetIcon(const QIcon & icon)

Notifier signal:

voidchanged()

iconText :QString

This property holds the action's descriptive icon text.

IfQToolBar::toolButtonStyle is set to a value that permits text to be displayed, the text defined held in this property appears as a label in the relevant tool button.

It also serves as the default text in menus and tooltips if the action has not been defined withsetText() orsetToolTip(), and will also be used in toolbar buttons if no icon has been defined usingsetIcon().

If the icon text is not explicitly set, the action's normal text will be used for the icon text.

By default, this property contains an empty string.

Access functions:

QStringiconText() const
voidsetIconText(const QString & text)

Notifier signal:

voidchanged()

See alsosetToolTip() andsetStatusTip().

iconVisibleInMenu :bool

This property holds whether or not an action should show an icon in a menu.

In some applications, it may make sense to have actions with icons in the toolbar, but not in menus. If true, the icon (if valid) is shown in the menu, when it is false, it is not shown.

The default is to follow whether theQt::AA_DontShowIconsInMenus attribute is set for the application. Explicitly settings this property overrides the presence (or abscence) of the attribute.

For example:

QApplication app(argc, argv);app.setAttribute(Qt::AA_DontShowIconsInMenus);// Icons are *no longer shown* in menus// ...QAction*myAction=newQAction();// ...myAction->setIcon(SomeIcon);myAction->setIconVisibleInMenu(true);// Icon *will* be shown in menus for *this* action.

This property was introduced in Qt 4.4.

Access functions:

boolisIconVisibleInMenu() const
voidsetIconVisibleInMenu(bool visible)

Notifier signal:

voidchanged()

See alsoQAction::icon andQApplication::setAttribute().

menuRole :MenuRole

This property holds the action's menu role.

This indicates what role the action serves in the application menu on Mac OS X. By default all action have theTextHeuristicRole, which means that the action is added based on its text (seeQMenuBar for more information).

The menu role can only be changed before the actions are put into the menu bar in Mac OS X (usually just before the first application window is shown).

This property was introduced in Qt 4.2.

Access functions:

MenuRolemenuRole() const
voidsetMenuRole(MenuRole menuRole)

Notifier signal:

voidchanged()

priority :Priority

This property holds the actions's priority in the user interface.

This property can be set to indicate how the action should be prioritized in the user interface.

For instance, when toolbars have theQt::ToolButtonTextBesideIcon mode set, then actions withLowPriority will not show the text labels.

This property was introduced in Qt 4.6.

Access functions:

Prioritypriority() const
voidsetPriority(Priority priority)

shortcut :QKeySequence

This property holds the action's primary shortcut key.

Valid keycodes for this property can be found inQt::Key andQt::Modifier. There is no default shortcut key.

Access functions:

QKeySequenceshortcut() const
voidsetShortcut(const QKeySequence & shortcut)

Notifier signal:

voidchanged()

shortcutContext :Qt::ShortcutContext

This property holds the context for the action's shortcut.

Valid values for this property can be found inQt::ShortcutContext. The default value isQt::WindowShortcut.

Access functions:

Qt::ShortcutContextshortcutContext() const
voidsetShortcutContext(Qt::ShortcutContext context)

Notifier signal:

voidchanged()

softKeyRole :SoftKeyRole

This property holds the action's softkey role.

This indicates what type of role this action describes in the softkey framework on platforms where such a framework is supported. Currently this is only supported on the Symbian platform.

The softkey role can be changed any time.

This property was introduced in Qt 4.6.

Access functions:

SoftKeyRolesoftKeyRole() const
voidsetSoftKeyRole(SoftKeyRole softKeyRole)

Notifier signal:

voidchanged()

statusTip :QString

This property holds the action's status tip.

The status tip is displayed on all status bars provided by the action's top-level parent widget.

By default, this property contains an empty string.

Access functions:

QStringstatusTip() const
voidsetStatusTip(const QString & statusTip)

Notifier signal:

voidchanged()

See alsosetToolTip() andshowStatusText().

text :QString

This property holds the action's descriptive text.

If the action is added to a menu, the menu option will consist of the icon (if there is one), the text, and the shortcut (if there is one). If the text is not explicitly set in the constructor, or by using setText(), the action's description icon text will be used as text. There is no default text.

Access functions:

QStringtext() const
voidsetText(const QString & text)

Notifier signal:

voidchanged()

See alsoiconText.

toolTip :QString

This property holds the action's tooltip.

This text is used for the tooltip. If no tooltip is specified, the action's text is used.

By default, this property contains the action's text.

Access functions:

QStringtoolTip() const
voidsetToolTip(const QString & tip)

Notifier signal:

voidchanged()

See alsosetStatusTip() andsetShortcut().

visible :bool

This property holds whether the action can be seen (e.g. in menus and toolbars).

Ifvisible is true the action can be seen (e.g. in menus and toolbars) and chosen by the user; ifvisible is false the action cannot be seen or chosen by the user.

Actions which are not visible arenot grayed out; they do not appear at all.

By default, this property is true (actions are visible).

Access functions:

boolisVisible() const
voidsetVisible(bool)

Notifier signal:

voidchanged()

whatsThis :QString

This property holds the action's "What's This?" help text.

The "What's This?" text is used to provide a brief description of the action. The text may contain rich text. There is no default "What's This?" text.

Access functions:

QStringwhatsThis() const
voidsetWhatsThis(const QString & what)

Notifier signal:

voidchanged()

See alsoQWhatsThis andQ3StyleSheet.

Member Function Documentation

QAction::QAction(QObject * parent)

Constructs an action withparent. Ifparent is an action group the action will be automatically inserted into the group.

QAction::QAction(constQString & text,QObject * parent)

Constructs an action with sometext andparent. Ifparent is an action group the action will be automatically inserted into the group.

The action uses a stripped version oftext (e.g. "&Menu Option..." becomes "Menu Option") as descriptive text for tool buttons. You can override this by setting a specific description withsetText(). The same text will be used for tooltips unless you specify a different text usingsetToolTip().

QAction::QAction(constQIcon & icon, constQString & text,QObject * parent)

Constructs an action with anicon and sometext andparent. Ifparent is an action group the action will be automatically inserted into the group.

The action uses a stripped version oftext (e.g. "&Menu Option..." becomes "Menu Option") as descriptive text for tool buttons. You can override this by setting a specific description withsetText(). The same text will be used for tooltips unless you specify a different text usingsetToolTip().

QAction::~QAction()

Destroys the object and frees allocated resources.

QActionGroup * QAction::actionGroup() const

Returns the action group for this action. If no action group manages this action then 0 will be returned.

See alsoQActionGroup andQAction::setActionGroup().

void QAction::activate(ActionEvent event)

Sends the relevant signals forActionEventevent.

Action based widgets use this API to cause theQAction to emit signals as well as emitting their own.

QList<QGraphicsWidget *> QAction::associatedGraphicsWidgets() const

Returns a list of widgets this action has been added to.

This function was introduced in Qt 4.5.

See alsoQWidget::addAction() andassociatedWidgets().

QList<QWidget *> QAction::associatedWidgets() const

Returns a list of widgets this action has been added to.

This function was introduced in Qt 4.2.

See alsoQWidget::addAction() andassociatedGraphicsWidgets().

QVariant QAction::data() const

Returns the user data as set inQAction::setData.

See alsosetData().

[virtual protected]bool QAction::event(QEvent * e)

Reimplemented fromQObject::event().

[slot]void QAction::hover()

This is a convenience slot that calls activate(Hover).

[signal]void QAction::hovered()

This signal is emitted when an action is highlighted by the user; for example, when the user pauses with the cursor over a menu option, toolbar button, or presses an action's shortcut key combination.

See alsoQAction::activate().

bool QAction::isSeparator() const

Returns true if this action is a separator action; otherwise it returns false.

See alsoQAction::setSeparator().

QMenu * QAction::menu() const

Returns the menu contained by this action. Actions that contain menus can be used to create menu items with submenus, or inserted into toolbars to create buttons with popup menus.

See alsosetMenu() andQMenu::addAction().

QWidget * QAction::parentWidget() const

Returns the parent widget.

void QAction::setActionGroup(QActionGroup * group)

Sets this action group togroup. The action will be automatically added to the group's list of actions.

Actions within the group will be mutually exclusive.

See alsoQActionGroup andQAction::actionGroup().

void QAction::setData(constQVariant & userData)

Sets the action's internal data to the givenuserData.

See alsodata().

[slot]void QAction::setDisabled(bool b)

This is a convenience function for theenabled property, that is useful for signals--slots connections. Ifb is true the action is disabled; otherwise it is enabled.

void QAction::setMenu(QMenu * menu)

Sets the menu contained by this action to the specifiedmenu.

See alsomenu().

void QAction::setSeparator(bool b)

Ifb is true then this action will be considered a separator.

How a separator is represented depends on the widget it is inserted into. Under most circumstances the text, submenu, and icon will be ignored for separator actions.

See alsoQAction::isSeparator().

void QAction::setShortcuts(constQList<QKeySequence> & shortcuts)

Setsshortcuts as the list of shortcuts that trigger the action. The first element of the list is the primary shortcut.

This function was introduced in Qt 4.2.

See alsoshortcuts() andshortcut.

void QAction::setShortcuts(QKeySequence::StandardKey key)

Sets a platform dependent list of shortcuts based on thekey. The result of calling this function will depend on the currently running platform. Note that more than one shortcut can assigned by this action. If only the primary shortcut is required, usesetShortcut instead.

This function was introduced in Qt 4.2.

See alsoQKeySequence::keyBindings().

QList<QKeySequence> QAction::shortcuts() const

Returns the list of shortcuts, with the primary shortcut as the first element of the list.

This function was introduced in Qt 4.2.

See alsosetShortcuts().

bool QAction::showStatusText(QWidget * widget = 0)

Updates the relevant status bar for thewidget specified by sending aQStatusTipEvent to its parent widget. Returns true if an event was sent; otherwise returns false.

If a null widget is specified, the event is sent to the action's parent.

See alsostatusTip.

[slot]void QAction::toggle()

This is a convenience function for thechecked property. Connect to it to change the checked state to its opposite state.

[slot]void QAction::trigger()

This is a convenience slot that calls activate(Trigger).

[signal]void QAction::triggered(bool checked = false)

This signal is emitted when an action is activated by the user; for example, when the user clicks a menu option, toolbar button, or presses an action's shortcut key combination, or whentrigger() was called. Notably, it isnot emitted whensetChecked() ortoggle() is called.

If the action is checkable,checked is true if the action is checked, or false if the action is unchecked.

See alsoQAction::activate(),QAction::toggled(), andchecked.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp