(5)本源代码来自于头文件 qaction . h :
#ifndef QACTION_H
#define QACTION_H
#include <QtGui/qtguiglobal.h>
#if QT_CONFIG(shortcut)
# include <QtGui/qkeysequence.h>
#endif
#include <QtGui/qicon.h>
#include <QtCore/qstring.h>
#include <QtCore/qvariant.h>
QT_REQUIRE_CONFIG(action);
QT_BEGIN_NAMESPACE //说明本类定义于 Qt 的全局命名空间
class QActionEvent;
class QActionGroup;
class QActionPrivate;
#ifndef QT_NO_DEBUG_STREAM //说明本类的指针可以用于打印
Q_GUI_EXPORT QDebug operator<<(QDebug, const QAction *);
#endif
class Q_GUI_EXPORT QAction : public QObject
{
Q_OBJECT
Q_DECLARE_PRIVATE(QAction)
//此属性表示 Action是否可见(例如在菜单和工具栏中),
//如果visible为true,则该 QAction可以被用户看到(例如在菜单和工具栏中)并选择;
//如果visible为false,则该 QAction不能被用户看到或选择。 //
//不可见的 QAction不会变灰;它们根本不会显示。 默认情况下,此属性为真(QAction可见)。
Q_PROPERTY(bool visible //表本菜单项是否可见
READ isVisible WRITE setVisible
NOTIFY visibleChanged FINAL)
//此属性包含动作的描述性文本。机器翻译总把 action控件翻译为操作,不再修改了,下同。
//如果该操作被添加到菜单中,菜单选项将包括图标(如果有的话)、文本和快捷键(如果有的话)。
//如果在构造函数中没有显式设置文本,或者使用setText()设置了文本,
//则该操作的描述图标文本 iconText将被用作文本。没有默认文本。
Q_PROPERTY(QString text //这是菜单栏里的按钮文本
READ text WRITE setText
NOTIFY changed) //若本属性为空,则用 iconText替代为本属性值
Q_PROPERTY(QString iconText //这是工具栏里的按钮文本
READ iconText WRITE setIconText
NOTIFY changed) //同一个信号函数
//此属性持有操作图标。
//在工具栏中,该图标用作工具按钮图标;在菜单中,它显示在菜单文本的左侧。没有默认图标。
//如果将空图标(Olcon::isNull())传递给此函数,则清除动作的图标。
Q_PROPERTY(QIcon icon //本按钮的图片
READ icon WRITE setIcon
NOTIFY changed)
//此属性表示操作是否应在菜单中显示图标。
//在某些应用程序中,在工具栏中添加带有图标的操作可能是有意义的,但在菜单中则不然。
//如果为真则图标(如果有效)将在菜单中显示,如果为假,则不会显示。
//默认情况下,会遵循是否为该应用程序设置了 Qt::AA DontShowlconsInMenus属性。
//显式设置此属性会覆盖该属性的存在(或不存在)。
Q_PROPERTY(bool iconVisibleInMenu //菜单栏里的按钮图标是否可见
READ isIconVisibleInMenu WRITE setIconVisibleInMenu
NOTIFY changed)
//此属性包含操作的提示信息。此文本用于工具提示。如果没有指定工具提示,则使用动作的文本。
//默认情况下,此属性包含操作的文字。 By default, this property contains the action's text.
Q_PROPERTY(QString toolTip //工具栏里的按钮提示词
READ toolTip WRITE setToolTip
NOTIFY changed)
Q_PROPERTY(QString statusTip //状态栏里的按钮提示词,可以写更长
READ statusTip WRITE setStatusTip
NOTIFY changed) //若状态栏包含有常备控件,则本提示词具备顶级显示的权力
//此属性持有操作的状态提示。状态提示显示在由动作的顶级父控件提供的所有状态栏上。
//默认情况下,此属性包含一个空字符串。
Q_PROPERTY(QString whatsThis //用于存储对本菜单项的帮助文本 help,内容
READ whatsThis WRITE setWhatsThis
NOTIFY changed)
//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.
//此属性持有动作的字体。字体属性用于渲染设置在QAction上的文本。
//字体可以被视为一个提示,因为根据应用程序和样式它不会在所有情况下被咨询。
//默认情况下,此属性包含应用程序的默认字体。
Q_PROPERTY(QFont font //按菜单项上的字体
READ font WRITE setFont
NOTIFY changed)
//class Q_GUI_EXPORT QKeySequence。
//Returns the primary shortcut. Note: Getter function for property shortcut.
Q_PROPERTY(QKeySequence shortcut //快捷键
READ shortcut WRITE setShortcut
NOTIFY changed)
//enum Qt::ShortcutContext { WidgetShortcut, WindowShortcut,
// ApplicationShortcut, WidgetWithChildrenShortcut };
Q_PROPERTY(Qt::ShortcutContext shortcutContext //识别快捷键的范围
READ shortcutContext WRITE setShortcutContext
NOTIFY changed)
//This property holds the context for the action's shortcut.
//Valid values for this property can be found in Qt::ShortcutContext.
//The default value is Qt::WindowShortcut.
//此属性表示操作是否应在上下文菜单中显示快捷方式。
//在某些应用程序中,在上下文菜单中设置具有快捷方式的操作是有意义的。
//如果为真,则在通过上下文菜单显示操作时显示快捷方式(如果有效),如果为假,则不显示。
//默认情况下,会遵循是否为该应用程序启用了 Qt::AA_DontShowShortcutsInContextMenus 属性。
//显式设置此属性将覆盖该属性。Explicitly setting this property overrides the attribute.
Q_PROPERTY(bool shortcutVisibleInContextMenu
READ isShortcutVisibleInContextMenu
WRITE setShortcutVisibleInContextMenu
NOTIFY changed) //表在右键快捷菜单中是否显示快捷键
//此属性表示操作是否可以自动重复。
//如果为真,则在系统上启用键盘自动重复的情况下,当按下键盘快捷键组合时,该操作将自动重复。默认值为真。
//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.
Q_PROPERTY(bool autoRepeat //表快捷键被频繁按下时,Action是否会连续触发
READ autoRepeat WRITE setAutoRepeat
NOTIFY changed)
//此属性持有动作的菜单角色。这表明该操作在macOS应用程序菜单中起什么作用。
//默认情况下,所有操作都具有TextHeuristicRole,这意味着操作是根据其文本添加的
//(请参阅OMenuBar以获取更多信息)。
//在 macOs 中,只能在将操作放入菜单栏之前(通常是在第一个应用程序窗口显示之前)更改菜单角色。
Q_PROPERTY(MenuRole menuRole //本属性仅仅用于 macOS,表本菜单项的作用
READ menuRole WRITE setMenuRole
NOTIFY changed)
//此属性表示操作是否启用。禁用操作不能被用户选择。它们不会从菜单或工具栏中消失,
//但会以显示它们不可用的方式显示。例如,它们可能仅以灰色显示。
//What's This? help on disabled actions is still available,
//只要设置了 QAction::whatsThis 属性。
//An action will be disabled when all widgets to which it is added
// (with QWidget::addAction()) are disabled or not visible.
//当动作被禁用时,无法通过其快捷键触发它。默认情况下,此属性为真(动作已启用)。
Q_PROPERTY(bool enabled //本按钮是否可用
READ isEnabled WRITE setEnabled
NOTIFY enabledChanged
RESET resetEnabled FINAL)
//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 a QActionGroup with the
// QActionGroup::exclusive property set to true.
Q_PROPERTY(bool checkable //本按钮是否可复选
READ isCheckable WRITE setCheckable
NOTIFY checkableChanged FINAL)
Q_PROPERTY(bool checked //本复选按钮是否被按下
READ isChecked WRITE setChecked
NOTIFY toggled) //复选按钮的按下与弹起有了变化
//This property holds whether the action is checked.
//Only checkable actions can be checked.
//By default, this is false (the action is unchecked).
//Note: The notifier signal for this property is toggled().
//As toggling a QAction changes its state, it will also emit a changed() signal.
//经测试:信号函数 toggled ( ) 确实还会触发一次 changed ( ) 信号函数
Q_PROPERTY(Priority priority //表本菜单项在 UI上的优先级,默认 Normal而非 Low。
READ priority WRITE setPriority) //枚举 Priority就定义在本类
//此属性在用户界面中保存操作的优先级。
//可以设置此属性以指示在用户界面中应如何对操作进行优先级排序。
//例如,当工具栏具有 Qt::ToolButtonTextBesidelcon 模式设置时,
//具有低优先级的 QAction将不会显示文本标签,而只显示图片。
//经测试:当工具栏具有 ToolButtonTextUnderlcon属性时,低优先级的工具栏按钮依然会显示文字。
private:
Q_DISABLE_COPY(QAction)
friend class QActionGroup; //这些全是友元类
friend class QWidget;
friend class QMenu;
friend class QMenuPrivate;
friend class QToolButton;
friend class QGraphicsWidget;
QObject * menuObject() const;
void setMenuObject(QObject * object);
protected:
bool event(QEvent *) override;
QAction(QActionPrivate & dd, QObject * parent);
public:
//Constructs an action with parent.
//If parent is an action group ,
//the action will be automatically inserted into the group.
explicit QAction(QObject * parent = nullptr);
//Constructs an action with some text and parent.
//If parent is an action group ,
//the action will be automatically inserted into the group.
explicit QAction(const QString & text, QObject * parent = nullptr);
//该操作使用文本的精简版本(例如,“&Menu Option...”变为“MenuOption”)作为工具按钮的描述性文本。
//您可以通过使用setText()设置特定的描述来覆盖此设置。
//除非您使用setToolTip()指定不同的文本,否则相同的文本将用于工具提示。
explicit QAction(const QIcon & icon, //参数 parent也可以不是窗体组件,而是 QActionGroup
const QString & text, QObject * parent = nullptr);
//Constructs an action with an icon and some text and parent.
~QAction();
//Q_PROPERTY(bool visible //表本菜单项是否可见
// READ isVisible WRITE setVisible
// NOTIFY visibleChanged FINAL)
bool isVisible() const;
public Q_SLOTS:
void setVisible(bool);
Q_SIGNALS:
void visibleChanged();
public:
//Q_PROPERTY(QString text //这是菜单栏里的按钮文本
// READ text WRITE setText
// NOTIFY changed) //若本属性为空,则用 iconText替代为本属性值
QString text() const;
void setText(const QString & text);
Q_SIGNALS:
void changed();
public:
//Q_PROPERTY(QString iconText //这是工具栏里的按钮文本
// READ iconText WRITE setIconText
// NOTIFY changed) //同一个信号函数
QString iconText() const;
void setIconText(const QString & text); //同样的信号函数,略了
//Q_PROPERTY(QIcon icon //本按钮的图片
// READ icon WRITE setIcon
// NOTIFY changed)
QIcon icon() const;
void setIcon(const QIcon & icon);
//Q_PROPERTY(bool iconVisibleInMenu //菜单栏里的按钮图标是否可见
// READ isIconVisibleInMenu WRITE setIconVisibleInMenu
// NOTIFY changed)
bool isIconVisibleInMenu() const;
void setIconVisibleInMenu(bool visible);
//Q_PROPERTY(QString toolTip //工具栏里的按钮提示词
// READ toolTip WRITE setToolTip
// NOTIFY changed)
QString toolTip() const;
void setToolTip(const QString & tip);
//Q_PROPERTY(QString statusTip //状态栏里的按钮提示词,可以写更长
// READ statusTip WRITE setStatusTip
// NOTIFY changed) //若状态栏包含有常备控件,则本提示词具备顶级显示的权力
QString statusTip() const;
void setStatusTip(const QString & statusTip);
//Q_PROPERTY(QString whatsThis //用于存储对本菜单项的帮助文本 help,内容
// READ whatsThis WRITE setWhatsThis
// NOTIFY changed)
QString whatsThis() const;
void setWhatsThis(const QString & what);
//Q_PROPERTY(QFont font //按菜单项上的字体
// READ font WRITE setFont
// NOTIFY changed)
QFont font() const;
void setFont(const QFont & font);
//Q_PROPERTY(QKeySequence shortcut //快捷键
// READ shortcut WRITE setShortcut
// NOTIFY changed)
QKeySequence shortcut() const;
void setShortcut(const QKeySequence & shortcut);
//Returns the list of shortcuts, //说明允许为一个按钮绑定多个快捷键。
// with the primary shortcut as the first element of the list.
QList<QKeySequence> shortcuts() const; //class QKeySequence {...};
void setShortcuts(const QList<QKeySequence> & shortcuts);
//Sets shortcuts as the list of shortcuts that trigger the action.
//The first element of the list is the primary shortcut.
void setShortcuts(QKeySequence::StandardKey key);
//Sets a platform dependent list of shortcuts based on the key.
//调用此函数的结果取决于当前运行的平台。
//请注意此操作可以分配多个快捷方式。如果只需要主快捷方式,请使用setShortcut。
//Q_PROPERTY(Qt::ShortcutContext shortcutContext //识别快捷键的范围
// READ shortcutContext WRITE setShortcutContext
// NOTIFY changed)
Qt::ShortcutContext shortcutContext() const;
void setShortcutContext(Qt::ShortcutContext context);
//Q_PROPERTY(bool shortcutVisibleInContextMenu
// READ isShortcutVisibleInContextMenu
// WRITE setShortcutVisibleInContextMenu
// NOTIFY changed) //表在右键快捷菜单中是否显示快捷键
bool isShortcutVisibleInContextMenu() const;
void setShortcutVisibleInContextMenu(bool show);
//Q_PROPERTY(bool autoRepeat //表快捷键被频繁按下时,Action是否会连续触发
// READ autoRepeat WRITE setAutoRepeat
// NOTIFY changed)
bool autoRepeat() const;
void setAutoRepeat(bool);
// note this is copied into qplatformmenu.h, which must stay in sync
enum MenuRole {
NoRole = 0,
TextHeuristicRole,
ApplicationSpecificRole,
AboutQtRole,
AboutRole,
PreferencesRole,
QuitRole
};
Q_ENUM( MenuRole )
//Q_PROPERTY(MenuRole menuRole //本属性仅仅用于 macOS,表本菜单项的作用
// READ menuRole WRITE setMenuRole
// NOTIFY changed)
MenuRole menuRole() const;
void setMenuRole(MenuRole menuRole);
//Q_PROPERTY(bool enabled //本按钮是否可用
// READ isEnabled WRITE setEnabled
// NOTIFY enabledChanged
// RESET resetEnabled FINAL)
bool isEnabled() const;
public Q_SLOTS:
void setEnabled(bool);
Q_SIGNALS:
void enabledChanged(bool enabled);
public Q_SLOTS:
void resetEnabled();
public:
//Q_PROPERTY(bool checkable //本按钮是否可复选
// READ isCheckable WRITE setCheckable
// NOTIFY checkableChanged FINAL)
bool isCheckable() const;
void setCheckable(bool);
Q_SIGNALS:
void checkableChanged(bool checkable);
public:
//Q_PROPERTY(bool checked //本复选按钮是否被按下
// READ isChecked WRITE setChecked
// NOTIFY toggled) //复选按钮的按下与弹起有了变化
bool isChecked() const;
public Q_SLOTS:
void setChecked(bool);
Q_SIGNALS:
void toggled(bool);
public:
enum Priority {
LowPriority = 0,
NormalPriority = 128,
HighPriority = 256
};
Q_ENUM( Priority )
//Q_PROPERTY(Priority priority //表本菜单项在 UI上的优先级,默认 Normal而非 Low。
// READ priority WRITE setPriority) //枚举 Priority就定义在本类
Priority priority() const;
void setPriority(Priority priority);
//Returns true if this action is a separator action; otherwise it returns false.
bool isSeparator() const;
void setSeparator(bool b); //原来菜单栏里的分隔符也是 QAciton类型
//If b is true then this action will be considered a separator.
//分隔符的表示方式取决于它插入的组件。在大多数情况下,对于分隔符操作,文本、子菜单和图标将被忽略。
QVariant data() const; //Returns the user data as set in QAction::setData.
void setData(const QVariant & var); //看来可以为按钮关联一个任意类型的值
//Sets the action's internal data to the given var.
//Returns the action group for this action.
//If no action group manages this action, then nullptr will be returned.
QActionGroup * actionGroup() const;
void setActionGroup(QActionGroup * group);
//Sets this action group to group.
//The action will be automatically added to the group's list of actions.
//Actions within the group will be mutually exclusive.
//官方注释指出:属于同一组的 QAction将自动互斥
bool showStatusText(QObject * object = nullptr); //顾名思义,展示状态栏的文本
//Updates the relevant status bar for the
// UI represented by object by sending a QStatusTipEvent.
//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.
QList<QObject *> associatedObjects() const; //比如本按钮既在菜单栏,又在工具栏里。
//Returns a list of objects this action has been added to.
enum ActionEvent {
Trigger, //this will cause the QAction::triggered() signal to be emitted
Hover //this will cause the QAction::hovered() signal to be emitted.
};
void activate(ActionEvent event); //用于让QAction发射信号函数。本函数又被下面的函数调用
//Sends the relevant signals for ActionEvent event.
//Action-based widgets use this API to cause the QAction to emit
// signals as well as emitting their own.
public Q_SLOTS:
// void setEnabled(bool);
// void resetEnabled( );
// void setVisible(bool);
// void setChecked(bool);
inline void setDisabled(bool b) { setEnabled(!b); } //借助 setEnabled()实现本函数
//用程序的方法使复选框的选中状态发生改变。
void toggle (); //Connect to it to change the checked state to its opposite state.
void hover () { activate(Hover ); }
//This is a convenience slot that calls activate(Hover).
void trigger () { activate(Trigger); } //对按钮的实际触发操作,就叫 trigger
//This is a convenience slot that calls activate(Trigger).
Q_SIGNALS:
//这个信号在用户激活某个操作时发出:例如,当用户单击菜单选项、工具栏按钮或按下某个操作的快捷键组合时,
// 或者当调用trigger()时。值得注意的是,当调用setChecked()或toggle()时,它不会被释放。
void triggered(bool checked = false); //本信号适用于所有的按钮,单选、复选或普通按钮。
void hovered(); //用户对按钮的悬停操作。
//此信号在用户高亮显示某个操作时发出;
//例如,当用户将光标停留在菜单选项、工具栏按钮上或按下某个操作的快捷键组合时。
//void enabledChanged(bool enabled );
//void changed(); //当很多无关紧要的属性的值发生变化时,会触发本信号。
//void checkableChanged(bool checkable);
//void visibleChanged();
//void toggled(bool); //本属性只适用于复选按钮,具有复选状态的按钮。
//This signal is emitted whenever
// a checkable action changes its isChecked()status.
//This can be the result of a user interaction,
// or because setChecked() was called.
//As setChecked() changes the QAction,
// it emits changed() in addition to toggled().
//checked is true if the action is checked, or false if the action is unchecked.
}; //完结 class QAction : public QObject
QT_END_NAMESPACE
#endif // QACTION_H
(6)
谢谢