Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qradiobutton.h"
5#include "qapplication.h"
6#include "qbitmap.h"
7#if QT_CONFIG(buttongroup)
8#include "qbuttongroup.h"
9#endif
10#include "qstylepainter.h"
11#include "qstyle.h"
12#include "qstyleoption.h"
13#include "qevent.h"
14
15#include "private/qabstractbutton_p.h"
16
18
20{
21 Q_DECLARE_PUBLIC(QRadioButton)
22
23public:
25 void init();
27};
28
29/*
30 Initializes the radio button.
31*/
33{
34 Q_Q(QRadioButton);
35 q->setCheckable(true);
36 q->setAutoExclusive(true);
37 q->setMouseTracking(true);
38 q->setForegroundRole(QPalette::WindowText);
39 q->setAttribute(Qt::WA_MacShowFocusRect);
41}
42
98{
99 Q_D(QRadioButton);
100 d->init();
101}
102
109
117 : QRadioButton(parent)
118{
119 setText(text);
120}
121
130{
131 if (!option)
132 return;
133 Q_D(const QRadioButton);
134 option->initFrom(this);
135 option->text = d->text;
136 option->icon = d->icon;
137 option->iconSize = iconSize();
138 if (d->down)
140 option->state |= (d->checked) ? QStyle::State_On : QStyle::State_Off;
142 option->state.setFlag(QStyle::State_MouseOver, d->hovering);
143 }
144}
145
150{
151 Q_D(const QRadioButton);
152 if (d->sizeHint.isValid())
153 return d->sizeHint;
158 false, text()).size();
159 if (!opt.icon.isNull())
160 sz = QSize(sz.width() + opt.iconSize.width() + 4, qMax(sz.height(), opt.iconSize.height()));
161 d->sizeHint = style()->sizeFromContents(QStyle::CT_RadioButton, &opt, sz, this);
162 return d->sizeHint;
163}
164
169{
170 return sizeHint();
171}
172
182
187{
188 Q_D(QRadioButton);
190 bool hit = false;
191 if (underMouse())
192 hit = hitButton(e->position().toPoint());
193
194 if (hit != d->hovering) {
195 update();
196 d->hovering = hit;
197 }
198 }
199
201}
202
212
215{
216 Q_D(QRadioButton);
217 if (e->type() == QEvent::StyleChange
218#ifdef Q_OS_MAC
219 || e->type() == QEvent::MacSizeChange
220#endif
221 )
222 d->setLayoutItemMargins(QStyle::SE_RadioButtonLayoutItem);
223 return QAbstractButton::event(e);
224}
225
226
228
229#include "moc_qradiobutton.cpp"
The QAbstractButton class is the abstract base class of button widgets, providing functionality commo...
void mouseMoveEvent(QMouseEvent *e) override
\reimp
bool event(QEvent *e) override
\reimp
void setText(const QString &text)
QSize iconSize
the icon size used for this button.
QString text
the text shown on the button
\inmodule QtCore
Definition qcoreevent.h:45
@ StyleChange
Definition qcoreevent.h:136
@ MacSizeChange
Definition qcoreevent.h:217
Type type() const
Returns the event type.
Definition qcoreevent.h:304
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition qicon.cpp:1019
\inmodule QtGui
Definition qevent.h:196
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
@ WindowText
Definition qpalette.h:51
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:404
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QRadioButton widget provides a radio button with a text label.
virtual void initStyleOption(QStyleOptionButton *button) const
Initialize option with the values from this QRadioButton.
void paintEvent(QPaintEvent *) override
\reimp
QSize minimumSizeHint() const override
\reimp
~QRadioButton()
Destructor.
QRadioButton(QWidget *parent=nullptr)
Constructs a radio button with the given parent, but with no text or pixmap.
bool event(QEvent *e) override
\reimp
QSize sizeHint() const override
\reimp
void mouseMoveEvent(QMouseEvent *) override
\reimp
bool hitButton(const QPoint &) const override
\reimp
\inmodule QtCore\reentrant
Definition qrect.h:30
QPointF position() const
Returns the position of the point in this event, relative to the widget or item that received the eve...
Definition qevent.h:119
The QSizePolicy class is a layout attribute describing horizontal and vertical resizing policy.
Definition qsizepolicy.h:18
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:133
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:130
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\variable QStyleOptionHeaderV2::textElideMode
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget.
@ State_MouseOver
Definition qstyle.h:80
@ State_Sunken
Definition qstyle.h:69
@ State_Off
Definition qstyle.h:70
@ State_On
Definition qstyle.h:72
@ CT_RadioButton
Definition qstyle.h:549
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w=nullptr) const =0
Returns the size of the element described by the specified option and type, based on the provided con...
virtual QRect itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
Returns the area within the given rectangle in which to draw the provided text according to the speci...
Definition qstyle.cpp:510
@ CE_RadioButton
Definition qstyle.h:178
virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget=nullptr) const =0
Returns the sub-area for the given element as described in the provided style option.
@ SE_RadioButtonLayoutItem
Definition qstyle.h:295
@ SE_RadioButtonClickRect
Definition qstyle.h:254
void setLayoutItemMargins(int left, int top, int right, int bottom)
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
QPoint pos
the position of the widget within its parent widget
Definition qwidget.h:111
QFontMetrics fontMetrics() const
Returns the font metrics for the widget's current font.
Definition qwidget.h:847
void ensurePolished() const
Ensures that the widget and its children have been polished by QStyle (i.e., have a proper font and p...
void update()
Updates the widget unless updates are disabled or the widget is hidden.
QStyle * style() const
Definition qwidget.cpp:2600
bool underMouse() const
Returns true if the widget is under the mouse cursor; otherwise returns false.
Definition qwidget.h:859
bool testAttribute(Qt::WidgetAttribute) const
Returns true if attribute attribute is set on this widget; otherwise returns false.
Definition qwidget.h:910
QString text
QStyleOptionButton opt
Combined button and popup list for selecting options.
@ WA_Hover
Definition qnamespace.h:340
@ WA_MacShowFocusRect
Definition qnamespace.h:359
@ TextShowMnemonic
Definition qnamespace.h:173
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
GLuint GLenum option
unsigned int uint
Definition qtypes.h:34