qt QAction Class

Qt文档链接

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

以下摘自Qt文档,版本归原作者,笔者无版权,另外还可参考博文,这篇文章算是对Qt文档翻译吧:

https://www.cnblogs.com/weizhixiang/p/5866725.html

 

Public Functions

  QAction(const QIcon &icon, const QString &text, QObject *parent = nullptr)
  QAction(const QString &text, QObject *parent = nullptr)
  QAction(QObject *parent = nullptr)
virtual ~QAction()
QActionGroup * actionGroup() const
void activate(QAction::ActionEvent event)
QList associatedGraphicsWidgets() const
QList associatedWidgets() const
bool autoRepeat() const
QVariant data() const
QFont font() const
QIcon icon() const
QString iconText() const
bool isCheckable() const
bool isChecked() const
bool isEnabled() const
bool isIconVisibleInMenu() const
bool isSeparator() const
bool isShortcutVisibleInContextMenu() const
bool isVisible() const
QMenu * menu() const
QAction::MenuRole menuRole() const
QWidget * parentWidget() const
QAction::Priority priority() const
void setActionGroup(QActionGroup *group)
void setAutoRepeat(bool)
void setCheckable(bool)
void setData(const QVariant &userData)
void setFont(const QFont &font)
void setIcon(const QIcon &icon)
void setIconText(const QString &text)
void setIconVisibleInMenu(bool visible)
void setMenu(QMenu *menu)
void setMenuRole(QAction::MenuRole menuRole)
void setPriority(QAction::Priority priority)
void setSeparator(bool b)
void setShortcut(const QKeySequence &shortcut)
void setShortcutContext(Qt::ShortcutContext context)
void setShortcutVisibleInContextMenu(bool show)
void setShortcuts(const QList &shortcuts)
void setShortcuts(QKeySequence::StandardKey key)
void setStatusTip(const QString &statusTip)
void setText(const QString &text)
void setToolTip(const QString &tip)
void setWhatsThis(const QString &what)
QKeySequence shortcut() const
Qt::ShortcutContext shortcutContext() const
QList shortcuts() const
bool showStatusText(QWidget *widget = nullptr)
QString statusTip() const
QString text() const
QString toolTip() const
QString whatsThis() const

Detailed Description

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:

    const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
    QAction *openAct = new QAction(openIcon, tr("&Open..."), this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setStatusTip(tr("Open an existing file"));
    connect(openAct, &QAction::triggered, this, &MainWindow::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 also QMenu, QToolBar, and Application Example.

你可能感兴趣的:(Qt学习)