1 | // Copyright (C) 2017 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 "qquickradiodelegate_p.h" |
5 | #include "qquickabstractbutton_p_p.h" |
6 | #include "qquickitemdelegate_p_p.h" |
7 | |
8 | #include <QtGui/qpa/qplatformtheme.h> |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | /*! |
13 | \qmltype RadioDelegate |
14 | \inherits ItemDelegate |
15 | //! \instantiates QQuickRadioDelegate |
16 | \inqmlmodule QtQuick.Controls |
17 | \since 5.7 |
18 | \ingroup qtquickcontrols-delegates |
19 | \brief Exclusive item delegate with a radio indicator that can be toggled on or off. |
20 | |
21 | \image qtquickcontrols-radiodelegate.gif |
22 | |
23 | RadioDelegate presents an item delegate that can be toggled on (checked) or |
24 | off (unchecked). Radio delegates are typically used to select one option |
25 | from a set of options. |
26 | |
27 | RadioDelegate inherits its API from \l ItemDelegate, which is inherited |
28 | from AbstractButton. For instance, you can set \l {AbstractButton::text}{text}, |
29 | and react to \l {AbstractButton::clicked}{clicks} using the AbstractButton |
30 | API. The state of the radio delegate can be set with the |
31 | \l {AbstractButton::}{checked} property. |
32 | |
33 | Radio delegates are \l {AbstractButton::autoExclusive}{auto-exclusive} |
34 | by default. Only one delegate can be checked at any time amongst radio |
35 | delegates that belong to the same parent item; checking another delegate |
36 | automatically unchecks the previously checked one. For radio delegates |
37 | that do not share a common parent, ButtonGroup can be used to manage |
38 | exclusivity. |
39 | |
40 | \l RadioButton is similar to RadioDelegate, except that it is typically |
41 | not used in views, but rather when there are only a few options, and often |
42 | with the requirement that each button is uniquely identifiable. |
43 | |
44 | \code |
45 | ButtonGroup { |
46 | id: buttonGroup |
47 | } |
48 | |
49 | ListView { |
50 | model: ["Option 1", "Option 2", "Option 3"] |
51 | delegate: RadioDelegate { |
52 | text: modelData |
53 | checked: index == 0 |
54 | ButtonGroup.group: buttonGroup |
55 | } |
56 | } |
57 | \endcode |
58 | |
59 | \sa {Customizing RadioDelegate}, {Delegate Controls}, RadioButton |
60 | */ |
61 | |
62 | class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickRadioDelegatePrivate : public QQuickItemDelegatePrivate |
63 | { |
64 | Q_DECLARE_PUBLIC(QQuickRadioDelegate) |
65 | |
66 | public: |
67 | QPalette defaultPalette() const override { return QQuickTheme::palette(scope: QQuickTheme::ListView); } |
68 | }; |
69 | |
70 | QQuickRadioDelegate::QQuickRadioDelegate(QQuickItem *parent) |
71 | : QQuickItemDelegate(*(new QQuickRadioDelegatePrivate), parent) |
72 | { |
73 | setCheckable(true); |
74 | setAutoExclusive(true); |
75 | } |
76 | |
77 | QFont QQuickRadioDelegate::defaultFont() const |
78 | { |
79 | return QQuickTheme::font(scope: QQuickTheme::ListView); |
80 | } |
81 | |
82 | #if QT_CONFIG(accessibility) |
83 | QAccessible::Role QQuickRadioDelegate::accessibleRole() const |
84 | { |
85 | return QAccessible::RadioButton; |
86 | } |
87 | #endif |
88 | |
89 | QT_END_NAMESPACE |
90 | |
91 | #include "moc_qquickradiodelegate_p.cpp" |
92 | |