QButton(3qt)

πŸ“¦ version 3.3.8b, in fedora 43, fedora 44

πŸ’– NAME πŸ’–

QButton β€” the abstract base class of button widgets, providing functionality common to buttons.

yep, that's this whole page (QButton(3qt), if you're being formal about it πŸŽ€). it's giving "mom of the button family" energy and honestly? iconic.


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <qbutton.h>

Inherits QWidget.

Inherited by QCheckBox, QPushButton, QRadioButton, and QToolButton.

✨ 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 & )
  • QKeySequence accel () const
  • virtual void setAccel ( const QKeySequence & )
  • 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)
  • void setAutoResize ( bool ) (obsolete)
  • bool autoRepeat () const
  • virtual void setAutoRepeat ( bool )
  • bool isExclusiveToggle () const
  • QButtonGroup * group () const

🫢 Public Slots 🫢

  • void animateClick ()
  • void toggle ()

πŸ”₯ Signals πŸ”₯

  • void pressed ()
  • void released ()
  • void clicked ()
  • void toggled ( bool on )
  • void stateChanged ( int state )

🌈 Properties 🌈

  • QKeySequence accel β€” the accelerator associated with the button
  • bool autoRepeat β€” whether autoRepeat is enabled
  • bool autoResize β€” whether autoResize is enabled (obsolete)
  • bool down β€” whether the button is pressed
  • bool exclusiveToggle β€” whether the button is an exclusive toggle (read only)
  • bool on β€” whether the button is toggled (read only)
  • QPixmap pixmap β€” the pixmap shown on the button
  • QString text β€” the text shown on the button
  • bool toggleButton β€” whether the button is a toggle button (read only)
  • ToggleState toggleState β€” the state of the toggle button (read only)
  • ToggleType toggleType β€” the type of toggle on the button (read only)

🧚 Protected Members 🧚

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

🌸 DESCRIPTION 🌸

okay so. The QButton class is the abstract base class of button widgets, providing functionality common to buttons. bestie it's the blueprint, not the build πŸ’…

If you actually want to create a button, use QPushButton.

QButton implements an abstract button, and lets subclasses decide how to reply to user actions and how to draw the button. main character delegation energy, honestly.

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 the "disabled" appearance when the button is disabled.

QButton provides most of the states used for buttons, and here's the rundown ✨:

  • isDown() tells you whether the button is pressed down.
  • isOn() tells you whether the button is on. Only toggle buttons can be switched on and off (see below).
  • isEnabled() tells you whether the button can be pressed by the user.
  • setAutoRepeat() sets whether the button will auto-repeat if the user holds it down.
  • setToggleButton() sets 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 and then released into the 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). no cap, that's the whole arc 🎒

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

QButton provides five signals:

  1. pressed() is emitted when the button is pressed. E.g. with the mouse or when animateClick() is called.
  2. released() is emitted when the button is released. E.g. when the mouse is released or the cursor is moved outside the widget.
  3. clicked() is emitted when the button is first pressed and then released when the accelerator key is typed, or when animateClick() is called.
  4. toggled(bool) is emitted when the state of a toggle button changes.
  5. stateChanged(int) is emitted when the state of a tristate toggle button changes.

If the button is a text button with an ampersand (&) in its text, QButton creates an automatic accelerator key. This code creates a push button labelled Ro<u>c</u>k & Roll (where the c is underlined). 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 because they have no automatic accelerator.

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 must reimplement at least drawButton() (to draw the button's outline) 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, 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. like, unless you're built different, don't 😭

See also QButtonGroup and Abstract Widget Classes.

πŸ’œ Member Type Documentation πŸ’œ

QButton::ToggleState

This enum defines the state of a toggle button.

  • QButton::Off β€” the button is in the "off" state
  • QButton::NoChange β€” the button is in the default/unchanged state
  • QButton::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:

  • QButton::SingleShot β€” pressing the button causes an action, then the button returns to the unpressed state.
  • QButton::Toggle β€” pressing the button toggles it between an On and an Off state.
  • QButton::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 called name with parent parent, using the widget flags f.

If parent is a QButtonGroup, this constructor calls QButtonGroup::insert(). automatic group chat enrollment, love that for it 🫢

QButton::~QButton ()

Destroys the button. rip little guy πŸ’€

QKeySequence QButton::accel () const

Returns the accelerator associated with the button. See the "accel" property for details.

void QButton::animateClick () [slot]

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

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

This function does nothing if the button is disabled.

See also accel.

bool QButton::autoRepeat () const

Returns TRUE if autoRepeat is enabled; otherwise returns FALSE. See the "autoRepeat" property for details.

bool QButton::autoResize () const

Returns TRUE if autoResize is enabled; otherwise returns FALSE. See the "autoResize" property for details.

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), when the accelerator key is typed or when animateClick() is called. This signal is not emitted if you call setDown().

The QButtonGroup::clicked() signal does the same job, if you want to connect several buttons to the same slot.

Warning: Don't launch a model dialog in response to this signal for a button that has autoRepeat turned on. fr fr, don't do it πŸ™ˆ

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

Examples:

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, these reimplementations should call drawButtonLabel().

See also drawButtonLabel() and paintEvent().

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

Draws the button text or pixmap.

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

See also drawButton() and paintEvent().

QButtonGroup * QButton::group () const

Returns the group that this button belongs to.

If the button is not a member of any QButtonGroup, this function returns 0. (solo era πŸ˜”)

See also QButtonGroup.

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

Returns TRUE if pos is inside the clickable button rectangle; otherwise returns FALSE.

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

bool QButton::isDown () const

Returns TRUE if the button is pressed; otherwise returns FALSE. See the "down" property for details.

bool QButton::isExclusiveToggle () const

Returns TRUE if the button is an exclusive toggle; otherwise returns FALSE. See the "exclusiveToggle" property for details.

bool QButton::isOn () const

Returns TRUE if the button is toggled; otherwise returns FALSE. See the "on" property for details.

bool QButton::isToggleButton () const

Returns TRUE if the button is a toggle button; otherwise returns FALSE. See the "toggleButton" property for details.

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

Handles paint events for buttons. Small and typically complex buttons 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 pixmap shown on the button. See the "pixmap" property for details.

void QButton::pressed () [signal]

This signal is emitted when the button is pressed down.

See also released() and clicked().

Examples:

void QButton::released () [signal]

This signal is emitted when the button is released.

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

void QButton::setAccel ( const QKeySequence & ) [virtual]

Sets the accelerator associated with the button. See the "accel" property for details.

void QButton::setAutoRepeat ( bool ) [virtual]

Sets whether autoRepeat is enabled. See the "autoRepeat" property for details.

void QButton::setAutoResize ( bool ) πŸ’…

Turns autoResize on or off, bestie. Wanna know what it actually does? Go peek at the "autoResize" property, it's all laid out there. ✨

void QButton::setDown ( bool ) [virtual]

Sets whether the button is pressed. Yeah, that's the whole thing β€” the "down" property has the deets. 🫢

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

Okay so this one flips the vibe of your button: if on is TRUE the state goes to On, otherwise it goes to Off. That's it, that's the function. 🌸

See also toggleState.

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

Sets the pixmap that gets shown on the button. Full story lives in the "pixmap" property. πŸŽ€

void QButton::setState ( ToggleState s ) [virtual protected]

Sets the toggle state of the button to s. And what can s be, you ask? Off, NoChange or On. That's the whole menu. πŸ’–

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

Sets the text shown on the button. The "text" property is where all the lore is. ✨

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

If b is TRUE, this button becomes a toggle button. If b is FALSE, this button becomes a command button. Glow-up or glow-down, your call. πŸ‘‘

See also toggleButton.

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

Sets the toggle type of the button to type.

And type can be set to SingleShot, Toggle and Tristate. Three flavours, pick your fighter. πŸ”₯

ToggleState QButton::state () const

Returns the state of the toggle button. The "toggleState" property is right there if you want more. 🌈

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

This signal goes off whenever a toggle button changes state. So state is On if the button is on, NoChange if it's in the "no change" state, or Off if the button is off. πŸ’€

And like, this can happen because of a user action, because the toggle() slot got activated, because of setState(), or because setOn() was called. Any of those. Not me listing every possible source πŸ’…

See also clicked() and QButton::ToggleState.

QString QButton::text () const

Returns the text shown on the button. "text" property, go read it, it's giving detail. πŸ¦‹

void QButton::toggle () [slot]

Toggles the state of a toggle button. Flip it and reverse it. πŸ™ˆ

See also on, setOn(), toggled(), and toggleButton.

ToggleType QButton::toggleType () const

Returns the type of toggle on the button. The "toggleType" property has everything you need. ⭐

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. Periodt.

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

See also clicked().

Example: listbox/listbox.cpp.


πŸ“ Property Documentation πŸ“

QKeySequence accel

This property holds the accelerator associated with the button.

It's 0 if there's no accelerator set. And if you set this property to 0, then any current accelerator gets removed. Poof, gone. πŸ’¨

Set this property's value with setAccel() and get this property's value with accel().

bool autoRepeat

This property holds whether autoRepeat is enabled.

If autoRepeat is enabled then the clicked() signal is emitted at regular intervals if the button is down. Heads up though: this property has no effect on toggle buttons. And autoRepeat is off by default, so it's lowkey just chilling. πŸ’œ

Set this property's value with setAutoRepeat() and get this property's value with autoRepeat().

bool autoResize

This property holds whether autoResize is enabled.

⚠️ Okay bestie, real talk: this property is obsolete. It's only provided to keep old source working. We strongly advise against using it in new code. Like, don't. 😭

If autoResize is enabled then the button will resize itself whenever the contents are changed.

Set this property's value with setAutoResize() and get this property's value with autoResize().

bool down

This property holds whether the button is pressed.

If this property is TRUE, the button is pressed down. But watch out β€” the pressed() and clicked() signals are not emitted if you set this property to TRUE. The default is FALSE. πŸ™ˆ

Set this property's value with setDown() and get this property's value with isDown().

bool exclusiveToggle

This property holds whether the button is an exclusive toggle.

If this property is TRUE and the button is in a QButtonGroup, the button can only be toggled off by another one being toggled on. The default is FALSE. ✨

Get this property's value with isExclusiveToggle().

bool on

This property holds whether the button is toggled.

This property should only be set for toggle buttons. Not command buttons. Just saying. πŸ’…

Get this property's value with isOn().

QPixmap pixmap

This property holds the pixmap shown on the button.

If the pixmap is monochrome (i.e. it is a QBitmap or its depth is 1) and it does not have a mask, this property will set the pixmap to be its own mask. The purpose of this is to draw transparent bitmaps which are important for toggle buttons, for example. Iconic behaviour honestly. 🌈

And heads up: pixmap() returns 0 if no pixmap was set.

Set this property's value with setPixmap() and get this property's value with pixmap().

QString text

This property holds the text shown on the button.

This property will return a QString::null if the button has no text. And okay this part is important β€” if the text has an ampersand (&) in it, then an accelerator is automatically created for it using the character that follows the '&' as the accelerator key. Any previous accelerator will be overwritten, or cleared if no accelerator is defined by the text. So don't be surprised when your old accel ghosts you. πŸ‘»

There is no default text.

Set this property's value with setText() and get this property's value with text().

bool toggleButton

This property holds whether the button is a toggle button.

The default value is FALSE. πŸ™ˆ

Get this property's value with isToggleButton().

ToggleState toggleState

This property holds the state of the toggle button.

⚠️ If this property is changed then it does not cause the button to be repainted. So like, don't expect a visual glow-up for free. πŸ’€

Get this property's value with state().

ToggleType toggleType

This property holds the type of toggle on the button.

The default toggle type is SingleShot. πŸŽ€

See also QButton::ToggleType.

Get this property's value with toggleType().


πŸ‘€ SEE ALSO πŸ‘€


Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.


✍️ AUTHOR ✍️

Generated automatically from the source code. (No human ghostwrote this, it literally spawned from the code itself, main character energy tbh πŸ’…)


πŸ› BUGS πŸ›

If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. 🫢

The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. So like, it's a vibe, not a promise. πŸ’–

If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qbutton.3qt) and the Qt version (3.3.8). Fr fr, include those, it helps so much. πŸ™


aaaand that's a wrap bestie!! πŸŽ€βœ¨ This lil doc came from the Trolltech AS girlies on 2 February 2007, and it's the QButton(3qt) page β€” 3.3.8 era, iconic behaviour, no cap. Go forth and toggle responsibly. πŸ’…πŸ‘‘πŸ¦‹

πŸ“¦ where this page comes from