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