QActionGroup的妙用

本文介绍Qt中QActionGroup类的用法,通过该类可以方便地管理一组操作项,确保任何时候只有一项被激活。文章展示了如何创建QActionGroup实例并添加QAction,同时介绍了关键的方法和信号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   在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 
boolisEnabled () const
boolisExclusive () const
boolisVisible () const
voidremoveAction ( QAction * action ) // 删除组中的一个QAction

Public Slots

voidsetDisabled ( bool b )
voidsetEnabled ( bool )
voidsetExclusive ( bool )
voidsetVisible ( bool )
以上几个槽函数很好理解。

Signals

voidhovered ( QAction * action )  //This signal is emitted when the given action in the action group ishighlighted by the user;  
voidtriggered ( QAction * action ) // This signal is emitted when the given action in the action group isactivated by the user;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值