Extending QAction by emitting additional Signals: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:HowTo]]<br />[[Category:snippets]]
[[Category:HowTo]]


'''English''' | [[ActionEmittingCheckedUncheckedSignals_German|Deutsch]]
{{DocLink|QAction}}s can be set up to be ''checkable'', that means that it will toggle its status between a checked and unchecked state. This is displayed, for instance, as a {{DocLink|QPushButton}} that remains repressed when clicked a first time. The problem with QAction is that it emits a single signal, <tt>triggered(bool)</tt>, 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.


= Extending QAction to provide a checked/unchecked signals =
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.


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.<br />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:


== Source Code ==
<code>
#ifndef CHECKABLEACTION_H
#define CHECKABLEACTION_H


The source code is really simple.<br />Here there is the header file:
#include <QAction>
#include <QDebug>


<code>#ifndef CHECKABLEACTION_H<br />#define CHECKABLEACTION_H
/*!
* This class represents an action that will emit
* distinct signals depending on the check of the
* action itself.
*/
class CheckableAction : public QAction
{
Q_OBJECT
public:
  explicit CheckableAction(QObject *parent = 0);


#include &lt;QAction&amp;gt;<br />#include &lt;QDebug&amp;gt;
signals:
  /*!
  * This signal is emitted each time the action is
  * checked.
  */
  void actionChecked();


/*!<br /> * This class represents an action that will emit<br /> * distinct signals depending on the check of the<br /> * action itself.<br /> */<br />class CheckableAction : public QAction<br />{<br /> Q_OBJECT<br />public:<br /> explicit CheckableAction(QObject '''parent = 0);
  /*!
<br />signals:
  * This signal is emitted each time the action is
<br /> /'''!<br /> * This signal is emitted each time the action is<br /> * checked.<br /> '''/<br /> void actionChecked();
  * unchecked.
<br /> /'''!<br /> * This signal is emitted each time the action is<br /> * unchecked.<br /> '''/<br /> void actionUnchecked();
  */
  void actionUnchecked();


public slots:


<br />public slots:
private slots:
  /*!
  * This slot is used to forward (i.e., emit) a triggered
  * event depending on the status of the action (i.e., if it
  * has been checked or not).
  * checked true if the action has been checked, false
  * otherwise
  */
  void slotForwardCheckSignal( bool checked );
};
 
#endif // CHECKABLEACTION_H
</code>
 
and here the implementation of the class
 
<code>
#include "checkableaction.h"
 
CheckableAction::CheckableAction(QObject *parent) :
QAction(parent)
{


<br />private slots:
  // auto connect the triggered event to the slot for
<br /> /'''!<br /> * This slot is used to forward (i.e., emit) a triggered<br /> * event depending on the status of the action (i.e., if it<br /> * has been checked or not).<br /> * checked true if the action has been checked, false<br /> * otherwise<br /> */<br /> void slotForwardCheckSignal( bool checked );
  // forwarding the check status
  connect( this, SIGNAL(triggered(bool)),
          this, SLOT(slotForwardCheckSignal(bool))
  );
}


};
void CheckableAction::slotForwardCheckSignal(bool checked)
{
qDebug() << "Action checked status " << checked;
 
// check the status of the action
if( ! isCheckable() ) {
  // the action cannot be checked, simply emit
  // a triggered event
  triggered();
} else
if( isChecked() ) {
  // the action is checked
  emit actionChecked();
}
else {
  // the action is unchecked
  emit actionUnchecked();
}
 
}
</code>


#endif // CHECKABLEACTION_H<br /></code>
==Usage Example==
The action can be used in the way a QAction is, for instance:


and here the implementation of the class
<code>
CheckableAction *myAction = new CheckableAction( this );
myAction->setText( tr("Action TEXT") );
myAction->setIcon( QIcon(":/actions/img/myaction.png") );
myAction->setStatusTip( tr("Check/Uncheck the action") );
myAction->setCheckable( true );


<code>#include &quot;checkableaction.h&amp;quot;
connect( myAction, SIGNAL(actionChecked()),
            this, SLOT(slotActionUncheked())
      );


CheckableAction::CheckableAction(QObject '''parent) :<br /> QAction(parent)<br />{<br /> // auto connect the triggered event to the slot for<br /> // forwarding the check status<br /> connect( this,<br /> SIGNAL (triggered(bool)),<br /> this,<br /> SLOT (slotForwardCheckSignal(bool)) );<br />}
connect( myAction, SIGNAL(actionUnchecked()),
<br />void CheckableAction::slotForwardCheckSignal(bool checked)<br />{<br /> qDebug() &lt;&lt; &quot;Action checked status &quot; &lt;&lt; checked;<br /> // check the status of the action<br /> if( ! isCheckable() )<br /> // the action cannot be checked, simply emit<br /> // a triggered event<br /> triggered();<br /> else<br /> if( isChecked() )<br /> // the action is checked<br /> emit actionChecked();<br /> else<br /> // the action is unchecked<br /> emit actionUnchecked();
            this, SLOT(slotActionCheked())
<br />}<br /></code>
      );
<br />h2. Usage Example
<br />The action can be used in the way a QAction is, for instance:
<br /><code><br />CheckableAction''' myAction = new CheckableAction( this );<br /> myAction-&gt;setText( tr(&quot;Action TEXT&amp;quot;) );<br /> myAction-&gt;setIcon( QIcon(&quot;:/actions/img/myaction.png&amp;quot;) );<br /> myAction-&gt;setStatusTip( tr(&quot;Check/Uncheck the action&amp;quot;) );<br /> myAction-&gt;setCheckable( true );<br /> connect( myAction,<br /> SIGNAL (actionChecked()),<br /> this,<br /> SLOT( slotActionUncheked()) );<br /> connect( myAction,<br /> SIGNAL (actionUnchecked()),<br /> this,<br /> SLOT (slotActionCheked()) );


</code>
</code>

Revision as of 02:38, 14 November 2020


QActions can be set up to be checkable, that means that it will toggle its status between a checked and unchecked state. This is displayed, for instance, as a QPushButton that remains repressed 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:

#ifndef CHECKABLEACTION_H
#define CHECKABLEACTION_H

#include <QAction>
#include <QDebug>

/*!
 * This class represents an action that will emit
 * distinct signals depending on the check of the
 * action itself.
 */
class CheckableAction : public QAction
{
 Q_OBJECT
 public:
  explicit CheckableAction(QObject *parent = 0);

 signals:
  /*!
   * This signal is emitted each time the action is
   * checked.
   */
  void actionChecked();

  /*!
   * This signal is emitted each time the action is
   * unchecked.
   */
  void actionUnchecked();

 public slots:

 private slots:
  /*!
   * This slot is used to forward (i.e., emit) a triggered
   * event depending on the status of the action (i.e., if it
   * has been checked or not).
   * checked true if the action has been checked, false
   * otherwise
   */
  void slotForwardCheckSignal( bool checked );
};

#endif // CHECKABLEACTION_H

and here the implementation of the class

#include "checkableaction.h"

CheckableAction::CheckableAction(QObject *parent) :
 QAction(parent)
 {

  // auto connect the triggered event to the slot for
  // forwarding the check status
  connect( this, SIGNAL(triggered(bool)),
           this, SLOT(slotForwardCheckSignal(bool))
  );
}

void CheckableAction::slotForwardCheckSignal(bool checked)
{
 qDebug() << "Action checked status " << checked;

 // check the status of the action
 if( ! isCheckable() ) {
  // the action cannot be checked, simply emit
  // a triggered event
  triggered();
 } else
 if( isChecked() ) {
  // the action is checked
  emit actionChecked();
 }
 else {
  // the action is unchecked
  emit actionUnchecked();
 }

}

Usage Example

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

CheckableAction *myAction = new CheckableAction( this );
myAction->setText( tr("Action TEXT") );
myAction->setIcon( QIcon(":/actions/img/myaction.png") );
myAction->setStatusTip( tr("Check/Uncheck the action") );
myAction->setCheckable( true );

connect( myAction, SIGNAL(actionChecked()),
             this, SLOT(slotActionUncheked())
       );

connect( myAction, SIGNAL(actionUnchecked()),
             this, SLOT(slotActionCheked())
       );