Extending QAction by emitting additional Signals: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
<div class="wiki-content qtcdk">
[[Category:HowTo]]<br />[[Category:snippets]]


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


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


<div class="cpp-qt geshi">
@#ifndef CHECKABLEACTION_H<br />#define CHECKABLEACTION_H


# <div class="de1"><span class="co2"><nowiki>#ifndef CHECKABLEACTION_H</nowiki></span></div>
#include &lt;QAction&amp;gt;<br />#include &lt;QDebug&amp;gt;
# <div class="de1"><span class="co2">#define CHECKABLEACTION_H</span></div>
# <div class="de1"> </div>
# <div class="de1"><span class="co2">#include &lt;QAction&gt;</span></div>
# <div class="de2"><span class="co2">#include &lt;QDebug&gt;</span></div>
# <div class="de1"> </div>
# <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>
/*!<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:
<br /> /'''!<br /> * This signal is emitted each time the action is<br /> * checked.<br /> '''/<br /> void actionChecked();
<br /> /'''!<br /> * This signal is emitted each time the action is<br /> * unchecked.<br /> '''/<br /> void actionUnchecked();


and here the implementation of the class


<div class="cpp-qt geshi">
<br />public slots:


# <div class="de1"><span class="co2"><nowiki>#include "checkableaction.h"</nowiki></span></div>
<br />private slots:
# <div class="de1"> </div>
<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 );
# <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>
};


==Usage Example==
#endif // CHECKABLEACTION_H<br />@


The action can be used in the way a QAction is, for instance:
and here the implementation of the class
 
<div class="cpp-qt geshi">
 
# <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>
# <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>
# <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>


===Categories:===
@#include &quot;checkableaction.h&amp;quot;


* [[:Category:HowTo|HowTo]]
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 />}
* [[:Category:snippets|snippets]]
<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();
<br />}<br />@
<br />h2. Usage Example
<br />The action can be used in the way a QAction is, for instance:
<br />@<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()) );


</div>
@

Revision as of 09:47, 23 February 2015


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:

@#ifndef CHECKABLEACTION_H
#define CHECKABLEACTION_H

  1. include <QAction&gt;
    #include <QDebug&gt;

/*!
* 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

@#include "checkableaction.h&quot;

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();
}
@
h2. 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&quot;) );
myAction->setIcon( QIcon(":/actions/img/myaction.png&quot;) );
myAction->setStatusTip( tr("Check/Uncheck the action&quot;) );
myAction->setCheckable( true );
connect( myAction,
SIGNAL (actionChecked()),
this,
SLOT( slotActionUncheked()) );
connect( myAction,
SIGNAL (actionUnchecked()),
this,
SLOT (slotActionCheked()) );

@