Extending QAction by emitting additional Signals

From Qt Wiki
Revision as of 09:22, 23 February 2015 by Maintenance script (talk | contribs)
Jump to navigation Jump to search

Extending QAction to provide a checked/unchecked signals

QAction can be set up to be checkable, that means that it will toggle its status between a checked and unchecked status. This is displayed, for instance, as a qpushbutton that remains pushed when clicked a first time. The problem with QAction is that it emits a single signal, triggered(bool), with the actual status of the action (if checked or not). This requires your slot to manually check the boolean flag to perform a specific behavior depending on the checked status. If you want your action to perform two different tasks without having to check the checked status of the action you can subclass the action implementing a couple of signals to emit events depending on the action internal status.
This can become very handy when you have got a set of actions that must be perform a different behavior when checked but must do all the same task when unchecked.

Source Code

The source code is really simple.
Here there is the header file:

  1. #ifndef CHECKABLEACTION_H
  2. #define CHECKABLEACTION_H
  3.  
  4. #include <QAction>
  5. #include <QDebug>
  6.  
  7. /*!
  8.   * This class represents an action that will emit
  9.   * distinct signals depending on the check of the
  10.   * action itself.
  11.   */
  12. class CheckableAction : public QAction
  13. {
  14.     Q_OBJECT
  15. public:
  16.     explicit CheckableAction(QObject *parent = 0);
  17.  
  18. signals:
  19.  
  20.     /*!
  21.       * This signal is emitted each time the action is
  22.       * checked.
  23.       */
  24.     void actionChecked();
  25.  
  26.     /*!
  27.       * This signal is emitted each time the action is
  28.       * unchecked.
  29.       */
  30.     void actionUnchecked();
  31.  
  32.  
  33.  
  34. public slots:
  35.  
  36.  
  37. private slots:
  38.  
  39.     /*!
  40.       * This slot is used to forward (i.e., emit) a triggered
  41.       * event depending on the status of the action (i.e., if it
  42.       * has been checked or not).
  43.       * \param checked true if the action has been checked, false
  44.       * otherwise
  45.       */
  46.     void slotForwardCheckSignal( bool checked );
  47.  
  48. };
  49.  
  50. #endif // CHECKABLEACTION_H

and here the implementation of the class

  1. #include "checkableaction.h"
  2.  
  3. CheckableAction::CheckableAction(QObject *parent) :
  4.     QAction(parent)
  5. {
  6.     // auto connect the triggered event to the slot for
  7.     // forwarding the check status
  8.     connect( this,
  9.              SIGNAL(triggered(bool)),
  10.              this,
  11.              SLOT(slotForwardCheckSignal(bool)) );
  12. }
  13.  
  14. void CheckableAction::slotForwardCheckSignal(bool checked)
  15. {
  16.     qDebug() << "Action checked status " << checked;
  17.     // check the status of the action
  18.     if( ! isCheckable() )
  19.         // the action cannot be checked, simply emit
  20.         // a triggered event
  21.         triggered();
  22.     else
  23.         if( isChecked() )
  24.             // the action is checked
  25.             emit actionChecked();
  26.         else
  27.             // the action is unchecked
  28.             emit actionUnchecked();
  29.  
  30. }

Usage Example

The action can be used in the way a QAction is, for instance:

  1. CheckableAction* myAction = new CheckableAction( this );
  2.     myAction->setText( tr("Action TEXT") );
  3.     myAction->setIcon( QIcon(":/actions/img/myaction.png") );
  4.     myAction->setStatusTip( tr("Check/Uncheck the action") );
  5.     myAction->setCheckable( true );
  6.     connect( myAction,
  7.              SIGNAL(actionChecked()),
  8.              this,
  9.              SLOT( slotActionUncheked()) );
  10.     connect( myAction,
  11.              SIGNAL(actionUnchecked()),
  12.              this,
  13.              SLOT(slotActionCheked()) );

Categories: