定义:
在应用程序中,可以通过菜单、工具栏按钮和键盘快捷键调用许多常用命令。 由于用户希望每个命令都以相同的方式执行,而不管使用的用户界面如何,将每个命令表示为一个动作(QAction)是很有用的。
调用QAction的官方例子:
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);
可以看到,QAction把用户的动作通过triggered信号绑定到指定的函数上。
接下来,分析一下QAction::triggered调用流程。
QAction继承于QObject。
当一个事件发生时,Qt 通过构造一个合适的 QEvent 子类的实例来创建一个事件对象来表示它,并通过调用它的 event() 函数将它传递给 QObject 的一个特定实例(或其子类之一)。(参考The Event System | Qt Core 5.15.8)
我们看下QAction的event实现
QAction::event(QEvent *e)
{
#if QT_CONFIG(shortcut)if (e->type() == QEvent::Shortcut) {QShortcutEvent *se = static_cast<QShortcutEvent *>(e);Q_ASSERT_X(se->key() == d_func()->shortcut || d_func()->alternateShortcuts.contains(se->key()),"QAction::event","Received shortcut event from incorrect shortcut");if (se->isAmbiguous())qWarning("QAction::event: Ambiguous shortcut overload: %s", se->key().toString(QKeySequence::NativeText).toLatin1().constData());elseactivate(Trigger);return true;}
#endifreturn QObject::event(e);
}
调用了activate函数
void QAction::activate(ActionEvent event)
{Q_D(QAction);if(event == Trigger) {QPointer<QObject> guard = this;if(d->checkable) {// the checked action of an exclusive group may not be uncheckedif (d->checked && (d->group&& d->group->exclusionPolicy() == QActionGroup::ExclusionPolicy::Exclusive&& d->group->checkedAction() == this)) {if (!guard.isNull())emit triggered(true);return;}setChecked(!d->checked);}if (!guard.isNull())emit triggered(d->checked);} else if(event == Hover) {emit hovered();}
}
触发triggered信号,之后会调用用户绑定的函数。
流程图如下: