Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Classes  ·  Annotées  ·  Hiérarchie  ·  Fonctions  ·  Structure  · 

QButton Class Reference


The QButton class is the abstract base class of button widgets, providing functionality common to buttons. More...

#include <qbutton.h>

Inherits QWidget.

Inherited by QCheckBox, QPushButton, QRadioButton and QToolButton.

List of all member functions.

Public Members

  • QButton ( QWidget * parent=0, const char * name=0, WFlags f=0 ) 
  • ~QButton () 
  • QString text () const
  • virtual void setText ( const QString & ) 
  • const QPixmap* pixmap () const
  • virtual void setPixmap ( const QPixmap & ) 
  • int accel () const
  • virtual void setAccel ( int ) 
  • bool isToggleButton () const
  • enum ToggleType { SingleShot, Toggle, Tristate }
  • ToggleType toggleType () const
  • virtual void setDown ( bool ) 
  • bool isDown () const
  • bool isOn () const
  • enum ToggleState { Off, NoChange, On }
  • ToggleState state () const
  • bool autoResize () const (obsolete)
  • virtual void setAutoResize ( bool ) (obsolete)
  • bool autoRepeat () const
  • virtual void setAutoRepeat ( bool ) 
  • bool isExclusiveToggle () const
  • QButtonGroup* group () const

Public Slots

Signals

Protected Members

  • void setToggleButton ( bool ) 
  • virtual void setToggleType ( ToggleType ) 
  • void setOn ( bool ) 
  • virtual void setState ( ToggleState ) 
  • virtual bool hitButton ( const QPoint & pos ) const
  • virtual void drawButton ( QPainter * ) 
  • virtual void drawButtonLabel ( QPainter * ) 
  • virtual void paintEvent ( QPaintEvent * ) 

Properties

TypeNameREADWRITEOptions
QStringtexttextsetText 
QPixmappixmappixmapsetPixmap 
intaccelaccelsetAccel 
booltoggleButtonisToggleButton  
ToggleTypetoggleTypetoggleType  
booldownisDownsetDown DESIGNABLE false
boolonisOn  
ToggleStatetoggleStatestate  
boolautoResizeautoResizesetAutoResize 
boolautoRepeatautoRepeatsetAutoRepeat 
boolexclusiveToggleisExclusiveToggle  

Detailed Description

The QButton class is the abstract base class of button widgets, providing functionality common to buttons.

The QButton class implements an abstract button, and lets subclasses specify how to reply to user actions and how to draw the button.

QButton provides both push and toggle buttons. The QRadioButton and QCheckBox classes provide only toggle buttons, QPushButton and QToolButton provide both toggle and push buttons.

Any button can have either a text or pixmap label. setText() sets the button to be a text button and setPixmap() sets it to be a pixmap button. The text/pixmap is manipulated as necessary to create "disabled" appearance when the button is disabled.

QButton provides most of the states used for buttons:

  • isDown() determines whether the button is pressed down.
  • isOn() determines whether the button is on. Only toggle buttons can be switched on and off (see below).
  • isEnabled() determines whether the button can be pressed by the user.
  • setAutoRepeat() determines whether the button will auto-repeat if the user holds it down.
  • setToggleButton() determines whether the button is a toggle button or not.

The difference between isDown() and isOn() is as follows: When the user clicks a toggle button to toggle it on, the button is first pressed, then released into on state. When the user clicks it again (to toggle it off) the button moves first to the pressed state, then to the off state (isOn() and isDown() are both FALSE).

Default buttons (as used in many dialogs) are provided by QPushButton::setDefault() and QPushButton::setAutoDefault().

QButton provides four signals:

  • pressed() is emitted when the left mouse button is pressed while the mouse cursor is inside the button.
  • released() is emitted when the left mouse button is released.
  • clicked() is emitted when the button is first pressed then released, or when the accelerator key is typed, or when animateClick() is called.
  • toggled(bool) is emitted when the state of a toggle button changes.
  • stateChanged(int) is emitted when the state of a tristate toggle button changes.

If the button is a text button with "&" in its text, QButton creates an automatic accelerator key. This code creates a push button labelled "Rock & Roll" (where the c is underscored). The button gets an automatic accelerator key, Alt-C:

    QPushButton *p = new QPushButton( "Ro&ck && Roll", this );

In this example, when the user presses Alt-C the button will call animateClick().

You can also set a custom accelerator using the setAccel() function. This is useful mostly for pixmap buttons since they have no automatic accelerator.

    QPushButton *p;
    p->setPixmap( QPixmap("print.png") );
    p->setAccel( ALT+Key_F7 );

All of the buttons provided by Qt (QPushButton, QToolButton, QCheckBox and QRadioButton) can display both text and pixmaps.

To subclass QButton, you have to reimplement at least drawButton() (to draw the button's outskirts) and drawButtonLabel() (to draw its text or pixmap). It is generally advisable to reimplement sizeHint() as well, and sometimes hitButton() (to determine whether a button press is within the button).

To reduce flickering the QButton::paintEvent() sets up a pixmap that the drawButton() function draws in. You should not reimplement paintEvent() for a subclass of QButton unless you want to take over all drawing.

See also QButtonGroup.

Examples: scrollview/scrollview.cpp


Member Type Documentation

QButton::ToggleState

This enum defines the state of a toggle button at any moment. The possible values are:
  • Off - the button is in the "off" state
  • NoChange - the button is in the default/unchanged state
  • On - the button is in the "on" state

QButton::ToggleType

This enum type defines what a button can do in response to a mouse/keyboard press:
  • SingleShot - pressing the button causes an action, then the button returns to the unpressed state.
  • Toggle - pressing the button toggles it between an On and and Off state.
  • Tristate - pressing the button cycles between the three states On, Off and NoChange


Member Function Documentation

QButton::QButton ( QWidget * parent=0, const char * name=0, WFlags f=0 )

Constructs a standard button with a parent widget and a name.

If parent is a QButtonGroup, this constructor calls QButtonGroup::insert().

QButton::~QButton ()

Destructs the button, deleting all its child widgets.

int QButton::accel () const

Returns the accelerator key currently set for the button, or 0 if no accelerator key has been set.

See also setAccel().

void QButton::animateClick () [slot]

Performs an animated click: The button is pressed and a short while later released.

pressed(), released(), clicked(), toggled(), and stateChanged() signals are emitted as appropriate.

This function does nothing if the button is disabled.

See also setAccel().

bool QButton::autoRepeat () const

Returns TRUE if the button is auto-repeating, else FALSE.

The default is FALSE.

See also setAutoRepeat().

bool QButton::autoResize () const

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

Strange pre-layout stuff.

Returns TRUE if auto-resizing is enabled, or FALSE if auto-resizing is disabled.

Auto-resizing is disabled by default.

See also setAutoResize().

void QButton::clicked () [signal]

This signal is emitted when the button is activated, i.e. first pressed down and then released when the mouse cursor is inside the button, or when the accelerator key is typed, or when animateClick() is called.

See also pressed(), released() and toggled().

void QButton::drawButton ( QPainter * ) [virtual protected]

Draws the button. The default implementation does nothing.

This virtual function is reimplemented by subclasses to draw real buttons. At some point in time, these reimplementations are supposed to call drawButtonLabel().

See also drawButtonLabel() and paintEvent().

Reimplemented in QToolButton, QPushButton, QRadioButton and QCheckBox.

void QButton::drawButtonLabel ( QPainter * ) [virtual protected]

Draws the button text or pixmap.

This virtual function is reimplemented by subclasses to draw real buttons. It's invoked by drawButton().

See also drawButton() and paintEvent().

Reimplemented in QToolButton, QCheckBox, QPushButton and QRadioButton.

void QButton::enabledChange ( bool e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QButton::focusInEvent ( QFocusEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

bool QButton::focusNextPrevChild ( bool next ) [virtual]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QButton::focusOutEvent ( QFocusEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

QButtonGroup * QButton::group () const

Returns a pointer to the group of which this button is a member.

If the button is not a member of any QButtonGroup, this function returns 0.

See also setGroup() and QButtonGroup.

bool QButton::hitButton ( const QPoint & pos ) const [virtual protected]

Returns TRUE if pos is inside the clickable button rectangle, or FALSE if it is outside.

Per default, the clickable area is the entire widget. Subclasses may reimplement it, though.

Reimplemented in QRadioButton.

bool QButton::isDown () const

Returns TRUE if the button pressed down, or FALSE if it is standing up.

See also setDown().

bool QButton::isExclusiveToggle () const

Returns TRUE if this button behaves exclusively inside a QButtonGroup. In that case, this button can only be toggled off by another button being toggled on.

bool QButton::isOn () const

Returns TRUE if this toggle button is switched on, or FALSE if it is switched off.

See also setOn() and isToggleButton().

Examples: qmag/qmag.cpp

bool QButton::isToggleButton () const

Returns TRUE if the button is a toggle button.

See also setToggleButton().

void QButton::keyPressEvent ( QKeyEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QButton::keyReleaseEvent ( QKeyEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QButton::mouseMoveEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QButton::mousePressEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QButton::mouseReleaseEvent ( QMouseEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QWidget.

void QButton::paintEvent ( QPaintEvent * event ) [virtual protected]

Handles paint events for buttons. Small and typically complex buttons (less than 300x100 pixels) are painted double-buffered to reduce flicker. The actually drawing is done in the virtual functions drawButton() and drawButtonLabel().

See also drawButton() and drawButtonLabel().

Reimplemented from QWidget.

const QPixmap * QButton::pixmap () const

Returns the button pixmap, or 0 if the button has no pixmap.

void QButton::pressed () [signal]

This signal is emitted when the button is pressed down.

See also released() and clicked().

void QButton::released () [signal]

This signal is emitted when the button is released.

See also pressed(), clicked() and toggled().

void QButton::setAccel ( int key ) [virtual]

Specifies an accelerator key for the button, or removes the accelerator if key is 0.

Setting a button text containing a shortcut character (for example the 'x' in E&xit) automatically defines an ALT+letter accelerator for the button. You only need to call this function in order to specify a custom accelerator.

Example:

    QPushButton *b1 = new QPushButton;
    b1->setText( "&OK" );               // sets accel ALT+'O'

    QPushButton *b2 = new QPushButton;
    b2->setPixmap( printIcon );         // pixmap instead of text
    b2->setAccel( CTRL+'P' );           // custom accel

See also accel(), setText() and QAccel.

void QButton::setAutoRepeat ( bool enable ) [virtual]

Turns on auto-repeat for the button if enable is TRUE, or turns it off if enable is FALSE.

When auto-repeat is enabled, the clicked() signal is emitted at regular intervals while the buttons is down.

setAutoRepeat() has no effect for toggle buttons.

See also isDown(), autoRepeat() and clicked().

void QButton::setAutoResize ( bool enable ) [virtual]

This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.

Strange pre-layout stuff.

Enables auto-resizing if enable is TRUE, or disables it if enable is FALSE.

When auto-resizing is enabled, the button will resize itself whenever the contents change.

See also autoResize() and adjustSize().

void QButton::setDown ( bool enable ) [virtual]

Sets the state of the button to pressed down if enable is TRUE or to standing up if enable is FALSE.

If the button is a toggle button, it is not toggled. Call toggle() as well if you need to do that. The pressed() and released() signals are not emitted by this function.

This method is provided in case you need to reimplement the mouse event handlers.

See also isDown(), setOn(), toggle() and toggled().

void QButton::setOn ( bool enable ) [protected]

Switches a toggle button on if enable is TRUE or off if enable is FALSE. This function should be called only for toggle buttons.

See also isOn() and isToggleButton().

void QButton::setPixmap ( const QPixmap & pixmap ) [virtual]

Sets the button to display pixmap

If pixmap is monochrome (i.e. it is a QBitmap or its depth is 1) and it does not have a mask, this function sets the pixmap to be its own mask. The purpose of this is to draw transparent bitmaps, which is important for e.g. toggle buttons.

See also pixmap(), setText(), setAccel() and QPixmap::mask().

void QButton::setState ( ToggleState t ) [protected]

This protected function sets the button state into state t but does not cause repainting.

See also setToggleType().

void QButton::setText ( const QString & text ) [virtual]

Sets the button to display text.

If the text contains an ampersand, QButton creates an automatic accelerator for it, such as Alt-c for "&Cancel".

See also text(), setPixmap(), setAccel() and QPixmap::mask().

Examples: layout/layout.cpp xform/xform.cpp qmag/qmag.cpp

void QButton::setToggleButton ( bool enable ) [protected]

Makes the button a toggle button if enable is TRUE, or a normal button if enable is FALSE.

Note that this function is protected. It is called from subclasses to enable the toggle functionality. QCheckBox and QRadioButton are toggle buttons. QPushButton is initially not a toggle button, but QPushButton::setToggleButton() can be called to create toggle buttons.

See also isToggleButton().

void QButton::setToggleType ( ToggleType type ) [protected]

Sets the type of toggling behavior. The default is SingleShot.

Subclasses use this, and present it with a more comfortable interface.

QButton::ToggleState QButton::state() const

Returns the state of the button.

See also ToggleState, ToggleType and setState().

void QButton::stateChanged ( int state ) [signal]

This signal is emitted whenever a toggle button changes status. state is 2 if the button is on, 1 if it is in the "no change" state or 0 if the button is off.

This may be the result of a user action, toggle() slot activation, setState(), or because setOn() was called.

See also clicked().

QString QButton::text () const

Returns the button text, or null string if the button has no text.

See also setText().

void QButton::toggle () [slot]

if this is a toggle button, toggles it.

QButton::ToggleType QButton::toggleType() const

Returns the current toggle type.

See also setToggleType().

void QButton::toggled ( bool on ) [signal]

This signal is emitted whenever a toggle button changes status. on is TRUE if the button is on, or FALSE if the button is off.

This may be the result of a user action, toggle() slot activation, or because setOn() was called.

See also clicked().


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2005 Trolltech, all rights reserved.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 2.3
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web