Difference between revisions of "Extending QAction by emitting additional Signals"

From Qt Wiki
Jump to: navigation, search
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<div class="wiki-content qtcdk">
+
[[Category:HowTo]]
  
=Extending QAction to provide a checked/unchecked signals=
+
{{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.
  
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.
+
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==
 
==Source Code==
 +
The source code is really simple. Here there is the header file:
  
The source code is really simple.<br /> Here there is the header file:
+
<code>
 +
#ifndef CHECKABLEACTION_H
 +
#define CHECKABLEACTION_H
  
<div class="cpp-qt geshi">
+
#include <QAction>
 +
#include <QDebug>
  
# <div class="de1"><span class="co2"><nowiki>#ifndef CHECKABLEACTION_H</nowiki></span></div>
+
/*!
# <div class="de1"><span class="co2">#define CHECKABLEACTION_H</span></div>
+
* This class represents an action that will emit
# <div class="de1"> </div>
+
* distinct signals depending on the check of the
# <div class="de1"><span class="co2">#include &lt;QAction&gt;</span></div>
+
* action itself.
# <div class="de2"><span class="co2">#include &lt;QDebug&gt;</span></div>
+
*/
# <div class="de1"> </div>
+
class CheckableAction : public QAction
# <div class="de1"><span class="coMULTI">/*!</span></div>
+
{
# <div class="de1"><span class="coMULTI">  * This class represents an action that will emit</span></div>
+
Q_OBJECT
# <div class="de1"><span class="coMULTI">  * distinct signals depending on the check of the</span></div>
+
public:
# <div class="de2"><span class="coMULTI">  * action itself.</span></div>
+
  explicit CheckableAction(QObject *parent = 0);
# <div class="de1"><span class="coMULTI">  */</span></div>
 
# <div class="de1"><span class="kw2">class</span> CheckableAction <span class="sy0">:</span> <span class="kw2">public</span> [http://doc.qt.io/QAction.html <span class="kw5">QAction</span>]</div>
 
# <div class="de1"><span class="br0">{</span></div>
 
# <div class="de1">    <span class="kw2">Q_OBJECT</span></div>
 
# <div class="de2"><span class="kw2">public</span><span class="sy0">:</span></div>
 
# <div class="de1">    <span class="kw2">explicit</span> CheckableAction<span class="br0">(</span>[http://doc.qt.io/QObject.html <span class="kw5">QObject</span>] <span class="sy0">*</span>parent <span class="sy0">=</span> <span class="nu0">0</span><span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1"> </div>
 
# <div class="de1"><span class="kw2">signals</span><span class="sy0">:</span></div>
 
# <div class="de1"> </div>
 
# <div class="de2">    <span class="coMULTI">/*!</span></div>
 
# <div class="de1"><span class="coMULTI">      * This signal is emitted each time the action is</span></div>
 
# <div class="de1"><span class="coMULTI">      * checked.</span></div>
 
# <div class="de1"><span class="coMULTI">      */</span></div>
 
# <div class="de1">    <span class="kw4">void</span> actionChecked<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de2"> </div>
 
# <div class="de1">    <span class="coMULTI">/*!</span></div>
 
# <div class="de1"><span class="coMULTI">      * This signal is emitted each time the action is</span></div>
 
# <div class="de1"><span class="coMULTI">      * unchecked.</span></div>
 
# <div class="de1"><span class="coMULTI">      */</span></div>
 
# <div class="de2">    <span class="kw4">void</span> actionUnchecked<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1"> </div>
 
# <div class="de1"> </div>
 
# <div class="de1"> </div>
 
# <div class="de1"><span class="kw2">public</span> <span class="kw2">slots</span><span class="sy0">:</span></div>
 
# <div class="de2"> </div>
 
# <div class="de1"> </div>
 
# <div class="de1"><span class="kw2">private</span> <span class="kw2">slots</span><span class="sy0">:</span></div>
 
# <div class="de1"> </div>
 
# <div class="de1">    <span class="coMULTI">/*!</span></div>
 
# <div class="de2"><span class="coMULTI">      * This slot is used to forward (i.e., emit) a triggered</span></div>
 
# <div class="de1"><span class="coMULTI">      * event depending on the status of the action (i.e., if it</span></div>
 
# <div class="de1"><span class="coMULTI">      * has been checked or not).</span></div>
 
# <div class="de1"><span class="coMULTI">      * \param checked true if the action has been checked, false</span></div>
 
# <div class="de1"><span class="coMULTI">      * otherwise</span></div>
 
# <div class="de2"><span class="coMULTI">      */</span></div>
 
# <div class="de1">    <span class="kw4">void</span> slotForwardCheckSignal<span class="br0">(</span> <span class="kw4">bool</span> checked <span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1"> </div>
 
# <div class="de1"><span class="br0">}</span><span class="sy0">;</span></div>
 
# <div class="de1"> </div>
 
# <div class="de2"><span class="co2">#endif // CHECKABLEACTION_H</span></div>
 
  
</div>
+
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
 +
</code>
  
 
and here the implementation of the class
 
and here the implementation of the class
  
<div class="cpp-qt geshi">
+
<code>
 +
#include "checkableaction.h"
  
# <div class="de1"><span class="co2"><nowiki>#include "checkableaction.h"</nowiki></span></div>
+
CheckableAction::CheckableAction(QObject *parent) :
# <div class="de1"> </div>
+
QAction(parent)
# <div class="de1">CheckableAction<span class="sy0">::</span><span class="me2">CheckableAction</span><span class="br0">(</span>[http://doc.qt.io/QObject.html <span class="kw5">QObject</span>] <span class="sy0">*</span>parent<span class="br0">)</span> <span class="sy0">:</span></div>
+
{
# <div class="de1">    [http://doc.qt.io/QAction.html <span class="kw5">QAction</span>]<span class="br0">(</span>parent<span class="br0">)</span></div>
 
# <div class="de2"><span class="br0">{</span></div>
 
# <div class="de1">    <span class="co1">// auto connect the triggered event to the slot for</span></div>
 
# <div class="de1">    <span class="co1">// forwarding the check status</span></div>
 
# <div class="de1">    <span class="kw2">connect</span><span class="br0">(</span> <span class="kw1">this</span><span class="sy0">,</span></div>
 
# <div class="de1">             SIGNAL<span class="br0">(</span>triggered<span class="br0">(</span><span class="kw4">bool</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">,</span></div>
 
# <div class="de2">             <span class="kw1">this</span><span class="sy0">,</span></div>
 
# <div class="de1">             SLOT<span class="br0">(</span>slotForwardCheckSignal<span class="br0">(</span><span class="kw4">bool</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1"><span class="br0">}</span></div>
 
# <div class="de1"> </div>
 
# <div class="de1"><span class="kw4">void</span> CheckableAction<span class="sy0">::</span><span class="me2">slotForwardCheckSignal</span><span class="br0">(</span><span class="kw4">bool</span> checked<span class="br0">)</span></div>
 
# <div class="de2"><span class="br0">{</span></div>
 
# <div class="de1">    <span class="kw2">qDebug</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0">&lt;&lt;</span> <span class="st0">"Action checked status "</span> <span class="sy0">&lt;&lt;</span> checked<span class="sy0">;</span></div>
 
# <div class="de1">    <span class="co1">// check the status of the action</span></div>
 
# <div class="de1">    <span class="kw1">if</span><span class="br0">(</span> <span class="sy0">!</span> isCheckable<span class="br0">(</span><span class="br0">)</span> <span class="br0">)</span></div>
 
# <div class="de1">        <span class="co1">// the action cannot be checked, simply emit</span></div>
 
# <div class="de2">        <span class="co1">// a triggered event</span></div>
 
# <div class="de1">        triggered<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1">    <span class="kw1">else</span></div>
 
# <div class="de1">        <span class="kw1">if</span><span class="br0">(</span> isChecked<span class="br0">(</span><span class="br0">)</span> <span class="br0">)</span></div>
 
# <div class="de1">            <span class="co1">// the action is checked</span></div>
 
# <div class="de2">            emit actionChecked<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1">        <span class="kw1">else</span></div>
 
# <div class="de1">            <span class="co1">// the action is unchecked</span></div>
 
# <div class="de1">            emit actionUnchecked<span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1"> </div>
 
# <div class="de2"><span class="br0">}</span></div>
 
  
</div>
+
  // auto connect the triggered event to the slot for
 +
  // forwarding the check status
 +
  connect( this, SIGNAL(triggered(bool)),
 +
          this, SLOT(slotForwardCheckSignal(bool))
 +
  );
 +
}
  
==Usage Example==
+
void CheckableAction::slotForwardCheckSignal(bool checked)
 +
{
 +
qDebug() << "Action checked status " << checked;
  
The action can be used in the way a QAction is, for instance:
+
// 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();
 +
}
  
<div class="cpp-qt geshi">
+
}
 +
</code>
  
# <div class="de1">CheckableAction<span class="sy0">*</span> myAction <span class="sy0">=</span> <span class="kw1">new</span> CheckableAction<span class="br0">(</span> <span class="kw1">this</span> <span class="br0">)</span><span class="sy0">;</span></div>
+
==Usage Example==
# <div class="de1">    myAction<span class="sy0">-&gt;</span><span class="me3">setText</span><span class="br0">(</span> tr<span class="br0">(</span><span class="st0">"Action TEXT"</span><span class="br0">)</span> <span class="br0">)</span><span class="sy0">;</span></div>
+
The action can be used in the way a QAction is, for instance:
# <div class="de1">    myAction<span class="sy0">-&gt;</span><span class="me3">setIcon</span><span class="br0">(</span> [http://doc.qt.io/QIcon.html <span class="kw5">QIcon</span>]<span class="br0">(</span><span class="st0">":/actions/img/myaction.png"</span><span class="br0">)</span> <span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1">    myAction<span class="sy0">-&gt;</span><span class="me3">setStatusTip</span><span class="br0">(</span> tr<span class="br0">(</span><span class="st0">"Check/Uncheck the action"</span><span class="br0">)</span> <span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de2">    myAction<span class="sy0">-&gt;</span><span class="me3">setCheckable</span><span class="br0">(</span> <span class="kw2">true</span> <span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de1">    <span class="kw2">connect</span><span class="br0">(</span> myAction<span class="sy0">,</span></div>
 
# <div class="de1">             SIGNAL<span class="br0">(</span>actionChecked<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">,</span></div>
 
# <div class="de1">             <span class="kw1">this</span><span class="sy0">,</span></div>
 
# <div class="de1">             SLOT<span class="br0">(</span> slotActionUncheked<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">)</span><span class="sy0">;</span></div>
 
# <div class="de2">    <span class="kw2">connect</span><span class="br0">(</span> myAction<span class="sy0">,</span></div>
 
# <div class="de1">             SIGNAL<span class="br0">(</span>actionUnchecked<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">,</span></div>
 
# <div class="de1">             <span class="kw1">this</span><span class="sy0">,</span></div>
 
# <div class="de1">             SLOT<span class="br0">(</span>slotActionCheked<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">)</span><span class="sy0">;</span></div>
 
  
</div>
+
<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 );
  
===Categories:===
+
connect( myAction, SIGNAL(actionChecked()),
 +
            this, SLOT(slotActionUncheked())
 +
      );
  
* [[:Category:HowTo|HowTo]]
+
connect( myAction, SIGNAL(actionUnchecked()),
* [[:Category:snippets|snippets]]
+
            this, SLOT(slotActionCheked())
 +
      );
  
</div>
+
</code>

Latest 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:

  1. ifndef CHECKABLEACTION_H
  2. define CHECKABLEACTION_H
  1. include <QAction>
  2. 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 );

};

  1. endif // CHECKABLEACTION_H

and here the implementation of the class

  1. 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())
      );