Extending QAction by emitting additional Signals: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
<div class="wiki-content qtcdk"> | |||
=Extending QAction to provide a checked/unchecked signals= | =Extending QAction to provide a checked/unchecked signals= | ||
Line 6: | Line 8: | ||
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"> | |||
# <div class="de1"><span class="co2"><nowiki>#ifndef CHECKABLEACTION_H</nowiki></span></div> | |||
# <div class="de1"><span class="co2">#define CHECKABLEACTION_H</span></div> | |||
# <div class="de1"> </div> | |||
# <div class="de1"><span class="co2">#include <QAction></span></div> | |||
# <div class="de2"><span class="co2">#include <QDebug></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> | |||
and here the implementation of the class | and here the implementation of the class | ||
<div class="cpp-qt geshi"> | |||
# <div class="de1"><span class="co2"><nowiki>#include "checkableaction.h"</nowiki></span></div> | |||
# <div class="de1"> </div> | |||
# <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"><<</span> <span class="st0">"Action checked status "</span> <span class="sy0"><<</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== | ==Usage Example== | ||
The action can be used in the way a QAction is, for instance: | The action can be used in the way a QAction is, for instance: | ||
<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">-></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">-></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">-></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">-></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:=== | ===Categories:=== | ||
Line 17: | Line 127: | ||
* [[:Category:HowTo|HowTo]] | * [[:Category:HowTo|HowTo]] | ||
* [[:Category:snippets|snippets]] | * [[:Category:snippets|snippets]] | ||
</div> |
Revision as of 09:22, 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
- #include <QAction>
- #include <QDebug>
- /*!
- * This class represents an action that will emit
- * distinct signals depending on the check of the
- * action itself.
- */
- {
- Q_OBJECT
- public:
- 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).
- * \param 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"
- {
- // 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->setStatusTip( tr("Check/Uncheck the action") );
- myAction->setCheckable( true );
- connect( myAction,
- SIGNAL(actionChecked()),
- this,
- SLOT( slotActionUncheked()) );
- connect( myAction,
- SIGNAL(actionUnchecked()),
- this,
- SLOT(slotActionCheked()) );