Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.
Extending QAction by emitting additional Signals
<textarea style='width:98%;' cols='90' rows='25' class='textarea' readonly="readonly">
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<br />#define CHECKABLEACTION_H
#include <QAction&gt;<br />#include <QDebug&gt;
/*!<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();
<br />public slots:
<br />private slots:
<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 );
};
#endif // CHECKABLEACTION_H<br />
and here the implementation of the class
#include "checkableaction.h&quot;
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 />}
<br />void CheckableAction::slotForwardCheckSignal(bool checked)<br />{<br /> qDebug() << "Action checked status " << 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 />
h2. Usage Example
The action can be used in the way a QAction is, for instance:
<br />CheckableAction''' myAction = new CheckableAction( this );<br /> myAction->setText( tr("Action TEXT&quot;) );<br /> myAction->setIcon( QIcon(":/actions/img/myaction.png&quot;) );<br /> myAction->setStatusTip( tr("Check/Uncheck the action&quot;) );<br /> myAction->setCheckable( true );<br /> connect( myAction,<br /> SIGNAL (actionChecked()),<br /> this,<br /> SLOT( slotActionUncheked()) );<br /> connect( myAction,<br /> SIGNAL (actionUnchecked()),<br /> this,<br /> SLOT (slotActionCheked()) );