QCheckBox Class Reference

The QCheckBox widget provides a checkbox with a text label.

  1. #include <QCheckBox>

Inherits: QAbstractButton.

Detailed Description

The QCheckBox widget provides a checkbox with a text label.

A QCheckBox is an option button that can be switched on (checked) or off (unchecked). Checkboxes are typically used to represent features in an application that can be enabled or disabled without affecting others, but different types of behavior can be implemented. For example, a QButtonGroup can be used to group check buttons logically, allowing exclusive checkboxes. However, QButtonGroup does not provide any visual representation.

The image below further illustrates the differences between exclusive and non-exclusive checkboxes.

Whenever a checkbox is checked or cleared it emits the signal stateChanged(). Connect to this signal if you want to trigger an action each time the checkbox changes state. You can use isChecked() to query whether or not a checkbox is checked.

In addition to the usual checked and unchecked states, QCheckBox optionally provides a third state to indicate "no change". This is useful whenever you need to give the user the option of neither checking nor unchecking a checkbox. If you need this third state, enable it with setTristate(), and use checkState() to query the current toggle state.

Just like QPushButton, a checkbox 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. For example:

  1. QCheckBox *checkbox = new QCheckBox("C&ase sensitive", this);

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

Important inherited functions: text(), setText(), text(), pixmap(), setPixmap(), accel(), setAccel(), isToggleButton(), setDown(), isDown(), isOn(), checkState(), autoRepeat(), isExclusiveToggle(), group(), setAutoRepeat(), toggle(), pressed(), released(), clicked(), toggled(), checkState(), and stateChanged().

Screenshot of a Macintosh style checkbox

Screenshot of a Macintosh style checkbox

A checkbox shown in the Macintosh widget style.
Screenshot of a Windows XP style checkbox

Screenshot of a Windows XP style checkbox

A checkbox shown in the Windows XP widget style.
Screenshot of a Plastique style checkbox

Screenshot of a Plastique style checkbox

A checkbox shown in the Plastique widget style.

See also QAbstractButton, QRadioButton, and GUI Design Handbook: Check Box.

Public Types

Toggle detailsenum QCheckBox::

ToggleStateToggleState { Off , NoChange , On Qt::Checked ...} { Off , NoChange , On Qt::Checked }Qt3-support

ConstantValueDescription
QCheckBox::Off Qt::Unchecked Use Qt::Unchecked instead.
QCheckBox::NoChange Qt::PartiallyChecked Use Qt::PartiallyChecked instead.
QCheckBox::On Qt::Checked Use Qt::Checked instead.

Look up this member in the source code.

Properties

Toggle detailstristatetristate : bool

This property holds whether the checkbox is a tri-state checkbox

The default is false; i.e. the checkbox has only two states.

Access functions:

Look up this member in the source code.

    Public Functions

    Toggle details QCheckBox

    QCheckBoxQCheckBox ( QWidget *parent=0 ) ( QWidget *parent=0 )

    Constructs a checkbox with the given parent, but with no text.

    parent is passed on to the QAbstractButton constructor.

    Look up this member in the source code.

    Toggle details QCheckBox

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

    Constructs a checkbox with the given parent and text.

    parent is passed on to the QAbstractButton constructor.

    Look up this member in the source code.

    Toggle details Qt::CheckState QCheckBox

    checkStatecheckState () ()const

    Returns the check box's check state. If you do not need tristate support, you can also use QAbstractButton::isChecked() which returns a boolean.

    See also setCheckState() and Qt::CheckState.

    Look up this member in the source code.

    Toggle details void QCheckBox

    checkStateSetcheckStateSet () () [virtual protected]

    Reimplemented from QAbstractButton::checkStateSet().

    Look up this member in the source code.

    Toggle details bool QCheckBox

    eventevent ( QEvent *e ) ( QEvent *e ) [virtual protected]

    Reimplemented from QObject::event().

    Look up this member in the source code.

    Toggle details bool QCheckBox

    hitButtonhitButton ( const QPoint &pos ) ( const QPoint &pos )const [virtual protected]

    Reimplemented from QAbstractButton::hitButton().

    Look up this member in the source code.

    Toggle details void QCheckBox

    initStyleOptioninitStyleOption ( QStyleOptionButton *option ) ( QStyleOptionButton *option )const[protected]

    Initializes option with the values from this QCheckBox. This method is useful for subclasses that require a QStyleOptionButton, but do not want to fill in all the information themselves.

    See also QStyleOption::initFrom().

    Look up this member in the source code.

    Toggle details void QCheckBox

    mouseMoveEventmouseMoveEvent ( QMouseEvent *e ) ( QMouseEvent *e ) [virtual protected]

    Reimplemented from QWidget::mouseMoveEvent().

    Look up this member in the source code.

    Toggle details void QCheckBox

    nextCheckStatenextCheckState () () [virtual protected]

    Reimplemented from QAbstractButton::nextCheckState().

    Look up this member in the source code.

    Toggle details void QCheckBox

    paintEventpaintEvent ( QPaintEvent * ) ( QPaintEvent * ) [virtual protected]

    Reimplemented from QWidget::paintEvent().

    Look up this member in the source code.

    Toggle details void QCheckBox

    setCheckStatesetCheckState ( Qt::CheckState state ) ( Qt::CheckState state )

    Sets the check box's check state to state. If you do not need tristate support, you can also use QAbstractButton::setChecked() which takes a boolean.

    See also checkState() and Qt::CheckState.

    Look up this member in the source code.

    Toggle details QSize QCheckBox

    sizeHintsizeHint () ()const [virtual]

    Reimplemented from QWidget::sizeHint().

    Look up this member in the source code.

    Toggle details QCheckBox

    QCheckBoxQCheckBox ( QWidget *parent , const char *name ) ( QWidget *parent , const char *name ) Qt3-support

    Use one of the constructors that doesn't take the name argument and then use setObjectName() instead.

    Look up this member in the source code.

    Toggle details QCheckBox

    QCheckBoxQCheckBox ( const QString &text , QWidget *parent , const char *name ...) ( const QString &text , QWidget *parent , const char *name ) Qt3-support

    Use one of the constructors that doesn't take the name argument and then use setObjectName() instead.

    Look up this member in the source code.

    Toggle details void QCheckBox

    setNoChangesetNoChange () () Qt3-support

    Use setCheckState() instead.

    Look up this member in the source code.

    Toggle details void QCheckBox

    setStatesetState ( ToggleState state ) ( ToggleState state ) Qt3-support

    Use setCheckState() instead.

    See also state().

    Look up this member in the source code.

    Toggle details ToggleState QCheckBox

    statestate () ()const Qt3-support

    Use checkState() instead.

    See also setState().

    Look up this member in the source code.

    Signals

    Toggle details void QCheckBox

    stateChangedstateChanged ( int state ) ( int state ) [signal]

    This signal is emitted whenever the check box's state changes, i.e. whenever the user checks or unchecks it.

    state contains the check box's new Qt::CheckState.

    Look up this member in the source code.

    Notes provided by the Qt Community

    No notes