在Qt中,有时我们经常要处理这样一种情况:在任何时间点,多个QAction只能有一个是激活的。遇到这种情况,我们通常是自己编码,通常信号-槽的连接来实现这类功能。殊不知,在Qt中有一个类,它能帮助我们自动实现这项功能,它就是QActionGroup。
QActionGroup类把一组操作项(actions)组合在一起。
以下是来自例子 Menus 中的一段示例代码:
alignmentGroup = new QActionGroup(this);
alignmentGroup->addAction(leftAlignAct);
alignmentGroup->addAction(rightAlignAct);
alignmentGroup->addAction(justifyAct);
alignmentGroup->addAction(centerAct);
leftAlignAct->setChecked(true);
QActionGroup类的功能很简单。
QActionGroup ( QObject * parent ) | |
~QActionGroup () | |
QList<QAction *> | actions () const |
QAction * | addAction ( QAction * action ) // 向组中添加一个已经存在的QAction |
QAction * | addAction ( const QString & text ) |
QAction * | addAction ( const QIcon & icon, const QString & text ) |
QAction * | checkedAction () const // 返回被checked的QAction |
bool | isEnabled () const |
bool | isExclusive () const |
bool | isVisible () const |
void | removeAction ( QAction * action ) // 删除组中的一个QAction |
Public Slots
void | setDisabled ( bool b ) |
void | setEnabled ( bool ) |
void | setExclusive ( bool ) |
void | setVisible ( bool ) |
Signals
void | hovered ( QAction * action ) //This signal is emitted when the given action in the action group ishighlighted by the user; |
void | triggered ( QAction * action ) // This signal is emitted when the given action in the action group isactivated by the user; |