Difference between revisions of "PushButton Based On Action"

From Qt Wiki
Jump to: navigation, search
(Add "cleanup" tag)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}

Revision as of 16:15, 3 March 2015

Creating a QPushButton component configured by a QAction

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:


  1. define ACTIONBUTTON_H
  1. include <QPushButton>
  2. include <QAction>


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 {



* The action associated to this button.
QAction actionOwner;


* Default constructor.
* 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 );


public slots:

* A slot to 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();


  1. 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) :



actionOwner = NULL;


void ActionButton::setAction(QAction *action) {

// if I've got already an action associated to the button

// remove all connections

if( actionOwner = NULL && actionOwner= action ){

disconnect( actionOwner,
SIGNAL (changed()),
SLOT (updateButtonStatusFromAction()) );

disconnect( this,

SIGNAL (clicked()),
SLOT (trigger()) );

// store the action

actionOwner = action;

// configure the button


// connect the action and the button

// so that when the action is changed the
// button is changed too!
connect( action,
SIGNAL (changed()),
SLOT (updateButtonStatusFromAction()));

// connect the button to the slot that forwards the

// signal to the action
connect( this,
SIGNAL (clicked()),
SLOT (trigger()) );


void ActionButton::updateButtonStatusFromAction() {

setText( actionOwner->text() );
setStatusTip( actionOwner->statusTip() );
setToolTip( actionOwner->toolTip() );
setIcon( actionOwner->icon() );
setEnabled( actionOwner->isEnabled() );
setCheckable( actionOwner->isCheckable() );
setChecked( actionOwner->isChecked());