PushButton Based On Action: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
m (Sorted includes)
 
(4 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:snippets]]
[[Category:snippets]]
{{DocLink|QPushButton}} does not allow itself to be configured by a {{DocLink|QAction}}. In the case when you need to share a QAction among different components like menus, buttons, and so on, you can simply extend the QPushButton class with code similar to the following:


= Creating a QPushButton component configured by a QAction =
<code>
 
#ifndef ACTIONBUTTON_H
QPushButton does not allow itself to be configured by a QAction. In the case when you need to share a QAction among different components like menus, buttons, and so on, you can simply extend the QPushButton class with code similar to the following:
 
<code>#ifndef ACTIONBUTTON_H
#define ACTIONBUTTON_H
#define ACTIONBUTTON_H


#include <QAction>
#include <QPushButton>
#include <QPushButton>
#include <QAction>


/*!
/*!
  '''An extension of a QPushButton that supports QAction.
  * An extension of a QPushButton that supports QAction.
''' This class represents a QPushButton extension that can be
* This class represents a QPushButton extension that can be
  * connected to an action and that configures itself depending
  * connected to an action and that configures itself depending
  * on the status of the action.
  * on the status of the action.
Line 25: Line 22:
class ActionButton : public QPushButton
class ActionButton : public QPushButton
{
{
Q_OBJECT
    Q_OBJECT
 
private:
private:
 
    // The action associated to this button.
/*!
    QAction *actionOwner = nullptr;
* The action associated to this button.
*/
QAction''' actionOwner;


public:
public:
/*!
    // Default constructor. Parent the widget parent of this button
* Default constructor.
    explicit ActionButton(QWidget *parent = nullptr);
* parent the widget parent of this button
*/
explicit ActionButton(QWidget '''parent = 0);
 
/*!
* Sets the action owner of this button, that is the action
* associated to the button. The button is configured immediatly
* depending on the action status and the button and the action
* are connected together so that when the action is changed the button
* is updated and when the button is clicked the action is triggered.
* action the action to associate to this button
*/
void setAction( QAction''' action );


signals:
    /*!
    * Set the action owner of this button, that is the action
    * associated to the button. The button is configured immediately
    * depending on the action status and the button and the action
    * are connected together so that when the action is changed the button
    * is updated and when the button is clicked the action is triggered.
    * action the action to associate to this button
    */
    void setAction(QAction *action);


public slots:
public slots:
/*!
    /*!
* A slot to update the button status depending on a change
    * Update the button status depending on a change
* on the action status. This slot is invoked each time the action
    * on the action status. This slot is invoked each time the action
* "changed" signal is emitted.
    * "changed" signal is emitted.
*/
    */
void updateButtonStatusFromAction();
    void updateButtonStatusFromAction();
 
};
};


Line 70: Line 57:
The following is the implementation:
The following is the implementation:


<code>#include "actionbutton.h"
<code>
#include "actionbutton.h"


ActionButton::ActionButton(QWidget *parent) :
ActionButton::ActionButton(QWidget *parent) :
QPushButton(parent)
    QPushButton(parent)
{
{}
actionOwner = NULL;
}


void ActionButton::setAction(QAction *action)
void ActionButton::setAction(QAction *action)
{
{
    // if I've got already an action associated to the button
    // remove all connections
    if (actionOwner && actionOwner != action) {
        disconnect(actionOwner, &QAction::changed,
                  this, &ActionButton::updateButtonStatusFromAction);
        disconnect(this, &ActionButton::clicked,
                  actionOwner, &QAction::trigger);
    }


// if I've got already an action associated to the button
    // store the action
// remove all connections
    actionOwner = action;
 
if( actionOwner [[Image:= NULL &amp;&amp; actionOwner |= NULL &amp;&amp; actionOwner ]]= action ){
disconnect( actionOwner,
SIGNAL (changed()),
this,
SLOT (updateButtonStatusFromAction()) );


disconnect( this,
    // configure the button
SIGNAL (clicked()),
    updateButtonStatusFromAction();
actionOwner,
SLOT (trigger()) );
}


// store the action
    // connect the action and the button
actionOwner = action;
    // so that when the action is changed the
    // button is changed too!
    connect(action, &QAction::changed,
            this, &ActionButton::updateButtonStatusFromAction);


// configure the button
    // connect the button to the slot that forwards the
updateButtonStatusFromAction();
    // signal to the action
 
    connect(this, &ActionButton::clicked,
// connect the action and the button
            actionOwner, &QAction::trigger);
// so that when the action is changed the
// button is changed too!
connect( action,
SIGNAL (changed()),
this,
SLOT (updateButtonStatusFromAction()));
 
// connect the button to the slot that forwards the
// signal to the action
connect( this,
SIGNAL (clicked()),
actionOwner,
SLOT (trigger()) );
}
}


void ActionButton::updateButtonStatusFromAction()
void ActionButton::updateButtonStatusFromAction()
{
{
setText( actionOwner->text() );
    if (!actionOwner)
setStatusTip( actionOwner->statusTip() );
        return;
setToolTip( actionOwner->toolTip() );
    setText(actionOwner->text());
setIcon( actionOwner->icon() );
    setStatusTip(actionOwner->statusTip());
setEnabled( actionOwner->isEnabled() );
    setToolTip(actionOwner->toolTip());
setCheckable( actionOwner->isCheckable() );
    setIcon(actionOwner->icon());
setChecked( actionOwner->isChecked());
    setEnabled(actionOwner->isEnabled());
 
    setCheckable(actionOwner->isCheckable());
    setChecked(actionOwner->isChecked());
}
}
</code>
</code>

Latest revision as of 18:53, 26 April 2018

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

QPushButton does not allow itself to be configured by a QAction. In the case when you need to share a QAction among different components like menus, buttons, and so on, you can simply extend the QPushButton class with code similar to the following:

#ifndef ACTIONBUTTON_H
#define ACTIONBUTTON_H

#include <QAction>
#include <QPushButton>

/*!
 * An extension of a QPushButton that supports QAction.
 * This class represents a QPushButton extension that can be
 * connected to an action and that configures itself depending
 * on the status of the action.
 * When the action changes its state, the button reflects
 * such changes, and when the button is clicked the action
 * is triggered.
 */
class ActionButton : public QPushButton
{
    Q_OBJECT
private:
    // The action associated to this button.
    QAction *actionOwner = nullptr;

public:
    // Default constructor. Parent the widget parent of this button
    explicit ActionButton(QWidget *parent = nullptr);

    /*!
     * Set the action owner of this button, that is the action
     * associated to the button. The button is configured immediately
     * depending on the action status and the button and the action
     * are connected together so that when the action is changed the button
     * is updated and when the button is clicked the action is triggered.
     * action the action to associate to this button
     */
    void setAction(QAction *action);

public slots:
    /*!
     * Update the button status depending on a change
     * on the action status. This slot is invoked each time the action
     * "changed" signal is emitted.
     */
    void updateButtonStatusFromAction();
};

#endif // ACTIONBUTTON_H

The idea is to extend QPushButton and to provide a setAction method that will immediately configure the button itself with the action data. After that, the button will connect to the action, so that when the button is clicked the action is triggered and vice-versa: when the action is changed the button is re-configured.

The following is the implementation:

#include "actionbutton.h"

ActionButton::ActionButton(QWidget *parent) :
    QPushButton(parent)
{}

void ActionButton::setAction(QAction *action)
{
    // if I've got already an action associated to the button
    // remove all connections
    if (actionOwner && actionOwner != action) {
        disconnect(actionOwner, &QAction::changed,
                   this, &ActionButton::updateButtonStatusFromAction);
        disconnect(this, &ActionButton::clicked,
                   actionOwner, &QAction::trigger);
    }

    // store the action
    actionOwner = action;

    // configure the button
    updateButtonStatusFromAction();

    // connect the action and the button
    // so that when the action is changed the
    // button is changed too!
    connect(action, &QAction::changed,
            this, &ActionButton::updateButtonStatusFromAction);

    // connect the button to the slot that forwards the
    // signal to the action
    connect(this, &ActionButton::clicked,
            actionOwner, &QAction::trigger);
}

void ActionButton::updateButtonStatusFromAction()
{
    if (!actionOwner)
        return;
    setText(actionOwner->text());
    setStatusTip(actionOwner->statusTip());
    setToolTip(actionOwner->toolTip());
    setIcon(actionOwner->icon());
    setEnabled(actionOwner->isEnabled());
    setCheckable(actionOwner->isCheckable());
    setChecked(actionOwner->isChecked());
}