Extending QAction by emitting additional Signals: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Undo revision 37540 by Fg1234 (talk))
Tag: Undo
 
(19 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<div class="wiki-content qtcdk">
[[Category:HowTo]]
== Introduction ==
{{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>


<div class="cpp-qt geshi">
/*!
* 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);


# <div class="de1"><span class="co2"><nowiki>#ifndef CHECKABLEACTION_H</nowiki></span></div>
signals:
# <div class="de1"><span class="co2">#define CHECKABLEACTION_H</span></div>
  /*!
# <div class="de1"> </div>
  * This signal is emitted each time the action is
# <div class="de1"><span class="co2">#include &lt;QAction&gt;</span></div>
  * checked.
# <div class="de2"><span class="co2">#include &lt;QDebug&gt;</span></div>
  */
# <div class="de1"> </div>
  void actionChecked();
# <div class="de1"><span class="coMULTI">/*!</span></div>
# <div class="de1"><span class="coMULTI">  * This class represents an action that will emit</span></div>
# <div class="de1"><span class="coMULTI">  * distinct signals depending on the check of the</span></div>
# <div class="de2"><span class="coMULTI">  * action itself.</span></div>
# <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>
  /*!
  * This signal is emitted each time the action is
  * unchecked.
  */
  void actionUnchecked();


and here the implementation of the class
public slots:


<div class="cpp-qt geshi">
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 );
};


# <div class="de1"><span class="co2"><nowiki>#include "checkableaction.h"</nowiki></span></div>
#endif // CHECKABLEACTION_H
# <div class="de1"> </div>
</code>
# <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>
and here the implementation of the class


==Usage Example==
<code>
#include "checkableaction.h"


The action can be used in the way a QAction is, for instance:
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))
  );
}


<div class="cpp-qt geshi">
void CheckableAction::slotForwardCheckSignal(bool checked)
{
qDebug() << "Action checked status " << checked;


# <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>
// check the status of the action
# <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>
if( ! isCheckable() ) {
# <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>
  // the action cannot be checked, simply emit
# <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>
  // a triggered event
# <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>
  triggered();
# <div class="de1">    <span class="kw2">connect</span><span class="br0">(</span> myAction<span class="sy0">,</span></div>
} else
# <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>
if( isChecked() ) {
# <div class="de1">             <span class="kw1">this</span><span class="sy0">,</span></div>
  // the action is checked
# <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>
  emit actionChecked();
# <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>
else {
# <div class="de1">             <span class="kw1">this</span><span class="sy0">,</span></div>
  // the action is unchecked
# <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>
  emit actionUnchecked();
}
}
</code>


</div>
== Usage Example ==
The action can be used in the way a QAction is, for instance:


===Categories:===
<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 );


* [[:Category:HowTo|HowTo]]
connect( myAction, SIGNAL(actionChecked()),
* [[:Category:snippets|snippets]]
            this, SLOT(slotActionUncheked())
      );


</div>
connect( myAction, SIGNAL(actionUnchecked()),
            this, SLOT(slotActionCheked())
      );
</code>

Latest revision as of 23:35, 22 November 2022

Introduction

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