1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the widgets module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qcheckbox.h"
30
#ifndef QT_NO_CHECKBOX
31
#include "qapplication.h"
34
#include "qstylepainter.h"
36
#include "qstyleoption.h"
38
#include "private/qabstractbutton_p.h"
40
class QCheckBoxPrivate : public QAbstractButtonPrivate
42
Q_DECLARE_PUBLIC(QCheckBox)
44
QCheckBoxPrivate():tristate(false), noChange(false){}
48
QStyleOptionButton getStyleOption() const;
53
\brief The QCheckBox widget provides a checkbox with a text label.
58
A QCheckBox is an option button that can be switched on (checked)
59
or off (unchecked). Checkboxes are typically used to represent
60
features in an application that can be enabled or disabled without
61
affecting others, but different types of behavior can be
64
A QButtonGroup can be used to group check buttons visually.
66
Whenever a checkbox is checked or cleared it emits the signal
67
stateChanged(). Connect to this signal if you want to trigger an
68
action each time the checkbox changes state. You can use
69
isChecked() to query whether or not a checkbox is checked.
71
In addition to the usual checked and unchecked states, QCheckBox
72
optionally provides a third state to indicate "no change". This
73
is useful whenever you need to give the user the option of neither
74
checking nor unchecking a checkbox. If you need this third state,
75
enable it with setTristate(), and use checkState() to query the current
78
Just like QPushButton, a checkbox button displays text, and
79
optionally a small icon. The text can be set in the constructor or
80
with setText(); the icon is set with setIcon().
82
Important inherited functions: text(), setText(), text(),
83
pixmap(), setPixmap(), accel(), setAccel(), isToggleButton(),
84
setDown(), isDown(), isOn(), checkState(), autoRepeat(),
85
isExclusiveToggle(), group(), setAutoRepeat(), toggle(),
86
pressed(), released(), clicked(), toggled(), checkState(), and
89
\inlineimage macintosh-checkbox.png Screenshot in Macintosh style
90
\inlineimage windows-checkbox.png Screenshot in Windows style
92
\sa QAbstractButton, QRadioButton, {fowler}{GUI Design Handbook: Check Box}
96
\enum QCheckBox::ToggleState
104
\fn void QCheckBox::stateChanged(int state)
106
This signal is emitted whenever the check box's state changes,
107
i.e. whenever the user checks or unchecks it.
109
\a state contains the check box's new ToggleState.
113
\property QCheckBox::tristate
114
\brief whether the checkbox is a tri-state checkbox
116
The default is false; i.e. the checkbox has only two states.
121
void QCheckBoxPrivate::init()
124
q->setCheckable(true);
127
QStyleOptionButton QCheckBoxPrivate::getStyleOption() const
129
Q_Q(const QCheckBox);
130
QStyleOptionButton opt;
133
opt.state |= QStyle::State_Sunken;
134
if (tristate && noChange)
135
opt.state |= QStyle::State_NoChange;
137
opt.state |= checked ? QStyle::State_On : QStyle::State_Off;
140
opt.iconSize = q->iconSize();
145
Constructs a checkbox with the given \a parent, but with no text.
147
The \a parent argument is passed on to the QAbstractButton constructor.
150
QCheckBox::QCheckBox(QWidget *parent)
151
: QAbstractButton (*new QCheckBoxPrivate, parent)
158
Constructs a checkbox with the given \a parent and \a text.
160
The \a parent argument is passed on to the QAbstractButton constructor.
163
QCheckBox::QCheckBox(const QString &text, QWidget *parent)
164
: QAbstractButton (*new QCheckBoxPrivate, parent)
171
void QCheckBox::setTristate(bool y)
177
bool QCheckBox::isTristate() const
179
Q_D(const QCheckBox);
185
Returns the check box's check state.
187
\sa setCheckState() Qt::CheckState
189
Qt::CheckState QCheckBox::checkState() const
191
Q_D(const QCheckBox);
192
if (d->tristate && d->noChange)
193
return Qt::PartiallyChecked;
194
return d->checked ? Qt::Checked : Qt::Unchecked;
198
Sets the check box's check state to \a state.
200
\sa checkState() Qt::CheckState
202
void QCheckBox::setCheckState(Qt::CheckState state)
205
if (state == Qt::PartiallyChecked) {
211
d->blockRefresh = true;
212
setChecked(state != Qt::Unchecked);
213
d->blockRefresh = false;
215
emit stateChanged(state);
221
QSize QCheckBox::sizeHint() const
223
Q_D(const QCheckBox);
225
QFontMetrics fm = fontMetrics();
226
QStyleOptionButton opt = d->getStyleOption();
227
QSize sz = style()->itemTextRect(fm, QRect(0, 0, 1, 1), Qt::TextShowMnemonic, false,
229
if (!opt.icon.isNull())
230
sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
231
return (style()->sizeFromContents(QStyle::CT_CheckBox, &opt, sz, this)
232
.expandedTo(QApplication::globalStrut()));
237
void QCheckBox::paintEvent(QPaintEvent *)
240
QStylePainter p(this);
241
QStyleOptionButton opt = d->getStyleOption();
242
p.drawControl(QStyle::CE_CheckBox, opt);
247
bool QCheckBox::hitButton(const QPoint &pos) const
249
Q_D(const QCheckBox);
250
QStyleOptionButton opt = d->getStyleOption();
251
return style()->subElementRect(QStyle::SE_CheckBoxClickRect, &opt, this).contains(pos);
255
void QCheckBox::checkStateSet()
259
emit stateChanged(checkState());
263
void QCheckBox::nextCheckState()
267
setCheckState((Qt::CheckState)((checkState() + 1) % 3));
269
QAbstractButton::nextCheckState();
270
QCheckBox::checkStateSet();
276
Use one of the constructors that doesn't take the \a name
277
argument and then use setObjectName() instead.
279
QCheckBox::QCheckBox(QWidget *parent, const char* name)
280
: QAbstractButton (*new QCheckBoxPrivate, parent)
288
Use one of the constructors that doesn't take the \a name
289
argument and then use setObjectName() instead.
291
QCheckBox::QCheckBox(const QString &text, QWidget *parent, const char* name)
292
: QAbstractButton (*new QCheckBoxPrivate, parent)
305
\fn void QCheckBox::setNoChange()
308
Use setCheckState() instead.
312
\fn void QCheckBox::setState(ToggleState state)
315
Use setCheckState() instead.
319
\fn QCheckBox::ToggleState QCheckBox::state() const
322
Use checkState() instead.