QRadioButton Class Reference

The QRadioButton widget provides a radio button with a text label. More...

 #include <QRadioButton>

Inherits: QAbstractButton.

Public Functions

QRadioButton ( QWidget * parent = 0 )
QRadioButton ( const QString & text, QWidget * parent = 0 )

Reimplemented Public Functions

virtual QSize minimumSizeHint () const
virtual QSize sizeHint () const

Protected Functions

void initStyleOption ( QStyleOptionButton * option ) const

Reimplemented Protected Functions

virtual bool event ( QEvent * e )
virtual bool hitButton ( const QPoint & pos ) const
virtual void mouseMoveEvent ( QMouseEvent * e )
virtual void paintEvent ( QPaintEvent * )

Additional Inherited Members

Detailed Description

The QRadioButton widget provides a radio button with a text label.

A QRadioButton is an option button that can be switched on (checked) or off (unchecked). Radio buttons typically present the user with a "one of many" choice. In a group of radio buttons only one radio button at a time can be checked; if the user selects another button, the previously selected button is switched off.

Radio buttons are autoExclusive by default. If auto-exclusive is enabled, radio buttons that belong to the same parent widget behave as if they were part of the same exclusive button group. If you need multiple exclusive button groups for radio buttons that belong to the same parent widget, put them into a QButtonGroup.

Whenever a button is switched on or off it emits the toggled() signal. Connect to this signal if you want to trigger an action each time the button changes state. Use isChecked() to see if a particular button is selected.

Just like QPushButton, a radio button displays text, and optionally a small icon. The icon is set with setIcon(). The text can be set in the constructor or with setText(). A shortcut key can be specified by preceding the preferred character with an ampersand in the text. For example:

 QRadioButton *button = new QRadioButton("Search from the &cursor", this);

In this example the shortcut is Alt+c. See the QShortcut documentation for details (to display an actual ampersand, use '&&').

Important inherited members: text(), setText(), text(), setDown(), isDown(), autoRepeat(), group(), setAutoRepeat(), toggle(), pressed(), released(), clicked(), and toggled().

Screenshot of a Plastique radio buttonA radio button shown in the Plastique widget style.
Screenshot of a Windows XP radio buttonA radio button shown in the Windows XP widget style.
Screenshot of a Macintosh radio buttonA radio button shown in the Macintosh widget style.

See also QPushButton, QToolButton, QCheckBox, GUI Design Handbook: Radio Button, and Group Box Example.

Member Function Documentation

QRadioButton::QRadioButton ( QWidget * parent = 0 )

Constructs a radio button with the given parent, but with no text or pixmap.

The parent argument is passed on to the QAbstractButton constructor.

QRadioButton::QRadioButton ( const QString & text, QWidget * parent = 0 )

Constructs a radio button with the given parent and a text string.

The parent argument is passed on to the QAbstractButton constructor.

bool QRadioButton::event ( QEvent * e ) [virtual protected]

Reimplemented from QObject::event().

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

Reimplemented from QAbstractButton::hitButton().

void QRadioButton::initStyleOption ( QStyleOptionButton * option ) const [protected]

Initialize option with the values from this QRadioButton. This method is useful for subclasses when they need a QStyleOptionButton, but don't want to fill in all the information themselves.

See also QStyleOption::initFrom().

QSize QRadioButton::minimumSizeHint () const [virtual]

Reimplemented from QWidget::minimumSizeHint().

This function was introduced in Qt 4.8.

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

Reimplemented from QWidget::mouseMoveEvent().

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

Reimplemented from QWidget::paintEvent().

QSize QRadioButton::sizeHint () const [virtual]

Reimplemented from QWidget::sizeHint().

Notes provided by the Qt Community
  • 0

Votes: 0

Coverage: Qt library 4.7, 4.8, 5.0

Picture of Volker Volker

Ant Farmer
35 notes

Nokia Certified Qt Developer

Make a single radio button uncheckable

A single radio button in a form (i.e. no other radio button in it) can by default switched on and off by clicking on them, they behave more like a QCheckBox in this case. You can force the button to be checked all the time by putting it into a QGroupBox like this:

  1. QButtonGroup *bg = new QButtonGroup(this);
  2. bg->addButton(ui->radioButton);
  3. ui->radioButton->setChecked(true);

However, instead of using a single radio button, considder using a checkbox instead. It is usually not a good idea to use UI elements for purposes they are not intended for. A radio button is an exclusive choice between a limited list of alternatives. A single choice is something else.