1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "window.h"
52 
53 #include <QCheckBox>
54 #include <QGridLayout>
55 #include <QGroupBox>
56 #include <QMenu>
57 #include <QPushButton>
58 #include <QRadioButton>
59 
60 //! [0]
Window(QWidget * parent)61 Window::Window(QWidget *parent)
62     : QWidget(parent)
63 {
64     QGridLayout *grid = new QGridLayout;
65     grid->addWidget(createFirstExclusiveGroup(), 0, 0);
66     grid->addWidget(createSecondExclusiveGroup(), 1, 0);
67     grid->addWidget(createNonExclusiveGroup(), 0, 1);
68     grid->addWidget(createPushButtonGroup(), 1, 1);
69     setLayout(grid);
70 
71     setWindowTitle(tr("Group Boxes"));
72     resize(480, 320);
73 }
74 //! [0]
75 
76 //! [1]
createFirstExclusiveGroup()77 QGroupBox *Window::createFirstExclusiveGroup()
78 {
79 //! [2]
80     QGroupBox *groupBox = new QGroupBox(tr("Exclusive Radio Buttons"));
81 
82     QRadioButton *radio1 = new QRadioButton(tr("&Radio button 1"));
83     QRadioButton *radio2 = new QRadioButton(tr("R&adio button 2"));
84     QRadioButton *radio3 = new QRadioButton(tr("Ra&dio button 3"));
85 
86     radio1->setChecked(true);
87 //! [1] //! [3]
88 
89     QVBoxLayout *vbox = new QVBoxLayout;
90     vbox->addWidget(radio1);
91     vbox->addWidget(radio2);
92     vbox->addWidget(radio3);
93     vbox->addStretch(1);
94     groupBox->setLayout(vbox);
95 //! [2]
96 
97     return groupBox;
98 }
99 //! [3]
100 
101 //! [4]
createSecondExclusiveGroup()102 QGroupBox *Window::createSecondExclusiveGroup()
103 {
104     QGroupBox *groupBox = new QGroupBox(tr("E&xclusive Radio Buttons"));
105     groupBox->setCheckable(true);
106     groupBox->setChecked(false);
107 //! [4]
108 
109 //! [5]
110     QRadioButton *radio1 = new QRadioButton(tr("Rad&io button 1"));
111     QRadioButton *radio2 = new QRadioButton(tr("Radi&o button 2"));
112     QRadioButton *radio3 = new QRadioButton(tr("Radio &button 3"));
113     radio1->setChecked(true);
114     QCheckBox *checkBox = new QCheckBox(tr("Ind&ependent checkbox"));
115     checkBox->setChecked(true);
116 //! [5]
117 
118 //! [6]
119     QVBoxLayout *vbox = new QVBoxLayout;
120     vbox->addWidget(radio1);
121     vbox->addWidget(radio2);
122     vbox->addWidget(radio3);
123     vbox->addWidget(checkBox);
124     vbox->addStretch(1);
125     groupBox->setLayout(vbox);
126 
127     return groupBox;
128 }
129 //! [6]
130 
131 //! [7]
createNonExclusiveGroup()132 QGroupBox *Window::createNonExclusiveGroup()
133 {
134     QGroupBox *groupBox = new QGroupBox(tr("Non-Exclusive Checkboxes"));
135     groupBox->setFlat(true);
136 //! [7]
137 
138 //! [8]
139     QCheckBox *checkBox1 = new QCheckBox(tr("&Checkbox 1"));
140     QCheckBox *checkBox2 = new QCheckBox(tr("C&heckbox 2"));
141     checkBox2->setChecked(true);
142     QCheckBox *tristateBox = new QCheckBox(tr("Tri-&state button"));
143     tristateBox->setTristate(true);
144 //! [8]
145     tristateBox->setCheckState(Qt::PartiallyChecked);
146 
147 //! [9]
148     QVBoxLayout *vbox = new QVBoxLayout;
149     vbox->addWidget(checkBox1);
150     vbox->addWidget(checkBox2);
151     vbox->addWidget(tristateBox);
152     vbox->addStretch(1);
153     groupBox->setLayout(vbox);
154 
155     return groupBox;
156 }
157 //! [9]
158 
159 //! [10]
createPushButtonGroup()160 QGroupBox *Window::createPushButtonGroup()
161 {
162     QGroupBox *groupBox = new QGroupBox(tr("&Push Buttons"));
163     groupBox->setCheckable(true);
164     groupBox->setChecked(true);
165 //! [10]
166 
167 //! [11]
168     QPushButton *pushButton = new QPushButton(tr("&Normal Button"));
169     QPushButton *toggleButton = new QPushButton(tr("&Toggle Button"));
170     toggleButton->setCheckable(true);
171     toggleButton->setChecked(true);
172     QPushButton *flatButton = new QPushButton(tr("&Flat Button"));
173     flatButton->setFlat(true);
174 //! [11]
175 
176 //! [12]
177     QPushButton *popupButton = new QPushButton(tr("Pop&up Button"));
178     QMenu *menu = new QMenu(this);
179     menu->addAction(tr("&First Item"));
180     menu->addAction(tr("&Second Item"));
181     menu->addAction(tr("&Third Item"));
182     menu->addAction(tr("F&ourth Item"));
183     popupButton->setMenu(menu);
184 //! [12]
185 
186     QAction *newAction = menu->addAction(tr("Submenu"));
187     QMenu *subMenu = new QMenu(tr("Popup Submenu"));
188     subMenu->addAction(tr("Item 1"));
189     subMenu->addAction(tr("Item 2"));
190     subMenu->addAction(tr("Item 3"));
191     newAction->setMenu(subMenu);
192 
193 //! [13]
194     QVBoxLayout *vbox = new QVBoxLayout;
195     vbox->addWidget(pushButton);
196     vbox->addWidget(toggleButton);
197     vbox->addWidget(flatButton);
198     vbox->addWidget(popupButton);
199     vbox->addStretch(1);
200     groupBox->setLayout(vbox);
201 
202     return groupBox;
203 }
204 //! [13]
205