1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example widgets/groupbox
30 \title Group Box Example
32 \brief The Group Box example shows how to use the different kinds of group
35 Group boxes are container widgets that organize buttons into groups,
36 both logically and on screen. They manage the interactions between
37 the user and the application so that you do not have to enforce
40 Group boxes are usually used to organize check boxes and radio
41 buttons into exclusive groups.
43 \image groupbox-example.png
45 The Group Boxes example consists of a single \c Window class that
46 is used to show four group boxes: an exclusive radio button group,
47 a non-exclusive checkbox group, an exclusive radio button group
48 with an enabling checkbox, and a group box with normal push buttons.
50 \section1 Window Class Definition
52 The \c Window class is a subclass of \c QWidget that is used to
53 display a number of group boxes. The class definition contains
54 functions to construct each group box and populate it with different
55 selections of button widgets:
57 \snippet examples/widgets/groupbox/window.h 0
59 In the example, the widget will be used as a top-level window, so
60 the constructor is defined so that we do not have to specify a parent
63 \section1 Window Class Implementation
65 The constructor creates a grid layout and fills it with each of the
66 group boxes that are to be displayed:
68 \snippet examples/widgets/groupbox/window.cpp 0
70 The functions used to create each group box each return a
71 QGroupBox to be inserted into the grid layout.
73 \snippet examples/widgets/groupbox/window.cpp 1
75 The first group box contains and manages three radio buttons. Since
76 the group box contains only radio buttons, it is exclusive by
77 default, so only one radio button can be checked at any given time.
78 We check the first radio button to ensure that the button group
79 contains one checked button.
81 \snippet examples/widgets/groupbox/window.cpp 3
83 We use a vertical layout within the group box to present the
84 buttons in the form of a vertical list, and return the group
85 box to the constructor.
87 The second group box is itself checkable, providing a convenient
88 way to disable all the buttons inside it. Initially, it is
89 unchecked, so the group box itself must be checked before any of
90 the radio buttons inside can be checked.
92 \snippet examples/widgets/groupbox/window.cpp 4
94 The group box contains three exclusive radio buttons, and an
95 independent checkbox. For consistency, one radio button must be
96 checked at all times, so we ensure that the first one is initially
99 \snippet examples/widgets/groupbox/window.cpp 5
101 The buttons are arranged in the same way as those in the first
104 \snippet examples/widgets/groupbox/window.cpp 6
106 The third group box is constructed with a "flat" style that is
107 better suited to certain types of dialog.
109 \snippet examples/widgets/groupbox/window.cpp 7
111 This group box contains only checkboxes, so it is non-exclusive by
112 default. This means that each checkbox can be checked independently
115 \snippet examples/widgets/groupbox/window.cpp 8
117 Again, we use a vertical layout within the group box to present
118 the buttons in the form of a vertical list.
120 \snippet examples/widgets/groupbox/window.cpp 9
122 The final group box contains only push buttons and, like the
123 second group box, it is checkable.
125 \snippet examples/widgets/groupbox/window.cpp 10
127 We create a normal button, a toggle button, and a flat push button:
129 \snippet examples/widgets/groupbox/window.cpp 11
131 Push buttons can be used to display popup menus. We create one, and
132 attach a simple menu to it:
134 \snippet examples/widgets/groupbox/window.cpp 12
136 Finally, we lay out the widgets vertically, and return the group box
139 \snippet examples/widgets/groupbox/window.cpp 13