Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qradiobutton.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qradiobutton.h"
6#include "qapplication.h"
7#include "qbitmap.h"
8#if QT_CONFIG(buttongroup)
9#include "qbuttongroup.h"
10#endif
11#include "qstylepainter.h"
12#include "qstyle.h"
13#include "qstyleoption.h"
14#include "qevent.h"
15
16#include "private/qabstractbutton_p.h"
17
19
21{
22 Q_DECLARE_PUBLIC(QRadioButton)
23
24public:
26 void init();
28};
29
30/*
31 Initializes the radio button.
32*/
34{
35 Q_Q(QRadioButton);
36 q->setCheckable(true);
37 q->setAutoExclusive(true);
38 q->setMouseTracking(true);
39 q->setForegroundRole(QPalette::WindowText);
40 q->setAttribute(Qt::WA_MacShowFocusRect);
41 setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem);
42}
43
44/*!
45 \class QRadioButton
46 \brief The QRadioButton widget provides a radio button with a text label.
47
48 \ingroup basicwidgets
49 \inmodule QtWidgets
50
51 \image fusion-radiobutton.png {Two radio buttons representing two options}
52
53 A QRadioButton is an option button that can be switched on (checked) or
54 off (unchecked). Radio buttons typically present the user with a "one
55 of many" choice. In a group of radio buttons, only one radio button at
56 a time can be checked; if the user selects another button, the
57 previously selected button is switched off.
58
59 Radio buttons are autoExclusive by default. If auto-exclusive is
60 enabled, radio buttons that belong to the same parent widget
61 behave as if they were part of the same exclusive button group. If
62 you need multiple exclusive button groups for radio buttons that
63 belong to the same parent widget, put them into a QButtonGroup.
64
65 Whenever a button is switched on or off, it emits the toggled() signal.
66 Connect to this signal if you want to trigger an action each time the
67 button changes state. Use isChecked() to see if a particular button is
68 selected.
69
70 Just like QPushButton, a radio button displays text, and
71 optionally a small icon. The icon is set with setIcon(). The text
72 can be set in the constructor or with setText(). A shortcut key
73 can be specified by preceding the preferred character with an
74 ampersand in the text. For example:
75
76 \snippet code/src_gui_widgets_qradiobutton.cpp 0
77
78 In this example the shortcut is \e{Alt+c}. See the \l
79 {QShortcut#mnemonic}{QShortcut} documentation for details. To
80 display an actual ampersand, use '&&'.
81
82 Important inherited members: text(), setText(), text(),
83 setDown(), isDown(), autoRepeat(), group(), setAutoRepeat(),
84 toggle(), pressed(), released(), clicked(), and toggled().
85
86 \sa QPushButton, QToolButton, QCheckBox
87*/
88
89
90/*!
91 Constructs a radio button with the given \a parent, but with no text or
92 pixmap.
93
94 The \a parent argument is passed on to the QAbstractButton constructor.
95*/
96
97QRadioButton::QRadioButton(QWidget *parent)
98 : QAbstractButton(*new QRadioButtonPrivate, parent)
99{
100 Q_D(QRadioButton);
101 d->init();
102}
103
104/*!
105 Destructor.
106*/
107QRadioButton::~QRadioButton()
108{
109}
110
111/*!
112 Constructs a radio button with the given \a parent and \a text string.
113
114 The \a parent argument is passed on to the QAbstractButton constructor.
115*/
116
117QRadioButton::QRadioButton(const QString &text, QWidget *parent)
118 : QRadioButton(parent)
119{
120 setText(text);
121}
122
123/*!
124 Initialize \a option with the values from this QRadioButton. This method is useful
125 for subclasses when they need a QStyleOptionButton, but don't want to fill
126 in all the information themselves.
127
128 \sa QStyleOption::initFrom()
129*/
130void QRadioButton::initStyleOption(QStyleOptionButton *option) const
131{
132 if (!option)
133 return;
134 Q_D(const QRadioButton);
135 option->initFrom(this);
136 option->text = d->text;
137 option->icon = d->icon;
138 option->iconSize = iconSize();
139 if (d->down)
140 option->state |= QStyle::State_Sunken;
141 option->state |= (d->checked) ? QStyle::State_On : QStyle::State_Off;
142 if (testAttribute(Qt::WA_Hover) && underMouse()) {
143 option->state.setFlag(QStyle::State_MouseOver, d->hovering);
144 }
145}
146
147/*!
148 \reimp
149*/
150QSize QRadioButton::sizeHint() const
151{
152 Q_D(const QRadioButton);
153 if (d->sizeHint.isValid())
154 return d->sizeHint;
155 ensurePolished();
156 QStyleOptionButton opt;
157 initStyleOption(&opt);
158 QSize sz = style()->itemTextRect(fontMetrics(), QRect(), Qt::TextShowMnemonic,
159 false, text()).size();
160 if (!opt.icon.isNull())
161 sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
162 d->sizeHint = style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this);
163 return d->sizeHint;
164}
165
166/*!
167 \reimp
168*/
169QSize QRadioButton::minimumSizeHint() const
170{
171 return sizeHint();
172}
173
174/*!
175 \reimp
176*/
177bool QRadioButton::hitButton(const QPoint &pos) const
178{
179 QStyleOptionButton opt;
180 initStyleOption(&opt);
181 return style()->subElementRect(QStyle::SE_RadioButtonClickRect, &opt, this).contains(pos);
182}
183
184/*!
185 \reimp
186*/
187void QRadioButton::mouseMoveEvent(QMouseEvent *e)
188{
189 Q_D(QRadioButton);
190 if (testAttribute(Qt::WA_Hover)) {
191 bool hit = false;
192 if (underMouse())
193 hit = hitButton(e->position().toPoint());
194
195 if (hit != d->hovering) {
196 update();
197 d->hovering = hit;
198 }
199 }
200
201 QAbstractButton::mouseMoveEvent(e);
202}
203
204/*!\reimp
205 */
206void QRadioButton::paintEvent(QPaintEvent *)
207{
208 QStylePainter p(this);
209 QStyleOptionButton opt;
210 initStyleOption(&opt);
211 p.drawControl(QStyle::CE_RadioButton, opt);
212}
213
214/*! \reimp */
215bool QRadioButton::event(QEvent *e)
216{
217 Q_D(QRadioButton);
218 if (e->type() == QEvent::StyleChange
219#ifdef Q_OS_MAC
220 || e->type() == QEvent::MacSizeChange
221#endif
222 )
223 d->setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem);
224 return QAbstractButton::event(e);
225}
226
227
228QT_END_NAMESPACE
229
230#include "moc_qradiobutton.cpp"