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 "qradiobutton.h"
30
#ifndef QT_NO_RADIOBUTTON
31
#include "qapplication.h"
33
#include "qbuttongroup.h"
34
#include "qstylepainter.h"
36
#include "qstyleoption.h"
40
\brief The QRadioButton widget provides a radio button with a text label.
45
A QRadioButton is an option button that can be switched on (checked) or
46
off (unchecked). Radio buttons typically present the user with a "one
47
of many" choice. In a group of radio buttons only one radio button at
48
a time can be checked; if the user selects another button, the
49
previously selected button is switched off.
51
Radio buttons are autoExclusive by default. If auto-exclusive is
52
enabled, radio buttons that belong to the same parent widget
53
behave as if they were part of the same exclusive button group. If
54
you need multiple exclusive button groups for radio buttons that
55
belong to the same parent widget, put them into a QButtonGroup.
57
Whenever a button is switched on or off it emits the toggled() signal.
58
Connect to this signal if you want to trigger an action each time the
59
button changes state. Otherwise, use isChecked() to see if a particular
62
Just like QPushButton, a radio button displays text, and
63
optionally a small icon. The text can be set in the constructor or
64
with setText(); the icon is set with setIcon().
66
Important inherited members: text(), setText(), text(),
67
setDown(), isDown(), autoRepeat(), group(), setAutoRepeat(),
68
toggle(), pressed(), released(), clicked(), and toggled().
70
\inlineimage macintosh-radiobutton.png Screenshot in Macintosh style
71
\inlineimage windows-radiobutton.png Screenshot in Windows style
73
\sa QPushButton, QToolButton, QCheckBox, {fowler}{GUI Design Handbook: Radio Button}
78
Initializes the radio button.
80
static void qRadioButtonInit(QRadioButton *button)
82
button->setCheckable(true);
83
button->setAutoExclusive(true);
88
Constructs a radio button with the given \a parent, but with no text or
91
The \a parent argument is passed on to the QAbstractButton constructor.
94
QRadioButton::QRadioButton(QWidget *parent)
95
: QAbstractButton(parent)
97
qRadioButtonInit(this);
101
Constructs a radio button with the given \a parent and a \a text string.
103
The \a parent argument is passed on to the QAbstractButton constructor.
106
QRadioButton::QRadioButton(const QString &text, QWidget *parent)
107
: QAbstractButton(parent)
109
qRadioButtonInit(this);
113
static QStyleOptionButton getStyleOption(const QRadioButton *btn)
115
QStyleOptionButton opt;
117
opt.text = btn->text();
118
opt.icon = btn->icon();
119
opt.iconSize = btn->iconSize();
121
opt.state |= QStyle::State_Sunken;
122
opt.state |= (btn->isChecked() ? QStyle::State_On : QStyle::State_Off);
129
QSize QRadioButton::sizeHint() const
132
QStyleOptionButton opt = getStyleOption(this);
133
QSize sz = style()->itemTextRect(fontMetrics(), QRect(0, 0, 1, 1), Qt::TextShowMnemonic,
134
false, text()).size();
135
if (!opt.icon.isNull())
136
sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
137
return (style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this).
138
expandedTo(QApplication::globalStrut()));
144
bool QRadioButton::hitButton(const QPoint &pos) const
146
QStyleOptionButton opt = getStyleOption(this);
147
return style()->subElementRect(QStyle::SE_RadioButtonClickRect, &opt, this).contains(pos);
152
void QRadioButton::paintEvent(QPaintEvent *)
154
QStylePainter p(this);
155
QStyleOptionButton opt = getStyleOption(this);
156
p.drawControl(QStyle::CE_RadioButton, opt);
161
Use one of the constructors that doesn't take the \a name
162
argument and then use setObjectName() instead.
164
QRadioButton::QRadioButton(QWidget *parent, const char* name)
165
:QAbstractButton(parent)
168
qRadioButtonInit(this);
172
Use one of the constructors that doesn't take the \a name
173
argument and then use setObjectName() instead.
175
QRadioButton::QRadioButton(const QString &text, QWidget *parent, const char* name)
176
:QAbstractButton(parent)
179
qRadioButtonInit(this);