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 "widgetgallery.h"
52#include "norwegianwoodstyle.h"
53
54#include <QApplication>
55#include <QCheckBox>
56#include <QComboBox>
57#include <QDateTimeEdit>
58#include <QDial>
59#include <QGridLayout>
60#include <QGroupBox>
61#include <QLabel>
62#include <QLineEdit>
63#include <QProgressBar>
64#include <QPushButton>
65#include <QRadioButton>
66#include <QScrollBar>
67#include <QSpinBox>
68#include <QStyle>
69#include <QStyleFactory>
70#include <QTableWidget>
71#include <QTextEdit>
72#include <QTimer>
73
74//! [0]
75WidgetGallery::WidgetGallery(QWidget *parent)
76 : QDialog(parent)
77{
78 styleComboBox = new QComboBox;
79 const QString defaultStyleName = QApplication::style()->objectName();
80 QStringList styleNames = QStyleFactory::keys();
81 styleNames.append(t: "NorwegianWood");
82 for (int i = 1, size = styleNames.size(); i < size; ++i) {
83 if (defaultStyleName.compare(s: styleNames.at(i), cs: Qt::CaseInsensitive) == 0) {
84 styleNames.swapItemsAt(i: 0, j: i);
85 break;
86 }
87 }
88 styleComboBox->addItems(texts: styleNames);
89
90 styleLabel = new QLabel(tr(s: "&Style:"));
91 styleLabel->setBuddy(styleComboBox);
92
93 useStylePaletteCheckBox = new QCheckBox(tr(s: "&Use style's standard palette"));
94 useStylePaletteCheckBox->setChecked(true);
95
96 disableWidgetsCheckBox = new QCheckBox(tr(s: "&Disable widgets"));
97
98 createTopLeftGroupBox();
99 createTopRightGroupBox();
100 createBottomLeftTabWidget();
101 createBottomRightGroupBox();
102 createProgressBar();
103//! [0]
104
105//! [1]
106 connect(sender: styleComboBox, signal: &QComboBox::textActivated,
107//! [1] //! [2]
108 receiver: this, slot: &WidgetGallery::changeStyle);
109 connect(sender: useStylePaletteCheckBox, signal: &QCheckBox::toggled,
110 receiver: this, slot: &WidgetGallery::changePalette);
111 connect(sender: disableWidgetsCheckBox, signal: &QCheckBox::toggled,
112 receiver: topLeftGroupBox, slot: &QGroupBox::setDisabled);
113 connect(sender: disableWidgetsCheckBox, signal: &QCheckBox::toggled,
114 receiver: topRightGroupBox, slot: &QGroupBox::setDisabled);
115 connect(sender: disableWidgetsCheckBox, signal: &QCheckBox::toggled,
116 receiver: bottomLeftTabWidget, slot: &QGroupBox::setDisabled);
117 connect(sender: disableWidgetsCheckBox, signal: &QCheckBox::toggled,
118 receiver: bottomRightGroupBox, slot: &QGroupBox::setDisabled);
119//! [2]
120
121//! [3]
122 QHBoxLayout *topLayout = new QHBoxLayout;
123//! [3] //! [4]
124 topLayout->addWidget(styleLabel);
125 topLayout->addWidget(styleComboBox);
126 topLayout->addStretch(stretch: 1);
127 topLayout->addWidget(useStylePaletteCheckBox);
128 topLayout->addWidget(disableWidgetsCheckBox);
129
130 QGridLayout *mainLayout = new QGridLayout;
131 mainLayout->addLayout(topLayout, row: 0, column: 0, rowSpan: 1, columnSpan: 2);
132 mainLayout->addWidget(topLeftGroupBox, row: 1, column: 0);
133 mainLayout->addWidget(topRightGroupBox, row: 1, column: 1);
134 mainLayout->addWidget(bottomLeftTabWidget, row: 2, column: 0);
135 mainLayout->addWidget(bottomRightGroupBox, row: 2, column: 1);
136 mainLayout->addWidget(progressBar, row: 3, column: 0, rowSpan: 1, columnSpan: 2);
137 mainLayout->setRowStretch(row: 1, stretch: 1);
138 mainLayout->setRowStretch(row: 2, stretch: 1);
139 mainLayout->setColumnStretch(column: 0, stretch: 1);
140 mainLayout->setColumnStretch(column: 1, stretch: 1);
141 setLayout(mainLayout);
142
143 setWindowTitle(tr(s: "Styles"));
144 styleChanged();
145}
146//! [4]
147
148//! [5]
149void WidgetGallery::changeStyle(const QString &styleName)
150//! [5] //! [6]
151{
152 if (styleName == "NorwegianWood")
153 QApplication::setStyle(new NorwegianWoodStyle);
154 else
155 QApplication::setStyle(QStyleFactory::create(styleName));
156}
157//! [6]
158
159//! [7]
160void WidgetGallery::changePalette()
161//! [7] //! [8]
162{
163 QApplication::setPalette(useStylePaletteCheckBox->isChecked() ?
164 QApplication::style()->standardPalette() : QPalette());
165}
166//! [8]
167
168void WidgetGallery::changeEvent(QEvent *event)
169{
170 if (event->type() == QEvent::StyleChange)
171 styleChanged();
172}
173
174void WidgetGallery::styleChanged()
175{
176 auto styleName = QApplication::style()->objectName();
177 for (int i = 0; i < styleComboBox->count(); ++i) {
178 if (QString::compare(s1: styleComboBox->itemText(index: i), s2: styleName, cs: Qt::CaseInsensitive) == 0) {
179 styleComboBox->setCurrentIndex(i);
180 break;
181 }
182 }
183
184 changePalette();
185}
186
187//! [9]
188void WidgetGallery::advanceProgressBar()
189//! [9] //! [10]
190{
191 int curVal = progressBar->value();
192 int maxVal = progressBar->maximum();
193 progressBar->setValue(curVal + (maxVal - curVal) / 100);
194}
195//! [10]
196
197//! [11]
198void WidgetGallery::createTopLeftGroupBox()
199//! [11] //! [12]
200{
201 topLeftGroupBox = new QGroupBox(tr(s: "Group 1"));
202
203 radioButton1 = new QRadioButton(tr(s: "Radio button 1"));
204 radioButton2 = new QRadioButton(tr(s: "Radio button 2"));
205 radioButton3 = new QRadioButton(tr(s: "Radio button 3"));
206 radioButton1->setChecked(true);
207
208 checkBox = new QCheckBox(tr(s: "Tri-state check box"));
209 checkBox->setTristate(true);
210 checkBox->setCheckState(Qt::PartiallyChecked);
211
212 QVBoxLayout *layout = new QVBoxLayout;
213 layout->addWidget(radioButton1);
214 layout->addWidget(radioButton2);
215 layout->addWidget(radioButton3);
216 layout->addWidget(checkBox);
217 layout->addStretch(stretch: 1);
218 topLeftGroupBox->setLayout(layout);
219}
220//! [12]
221
222void WidgetGallery::createTopRightGroupBox()
223{
224 topRightGroupBox = new QGroupBox(tr(s: "Group 2"));
225
226 defaultPushButton = new QPushButton(tr(s: "Default Push Button"));
227 defaultPushButton->setDefault(true);
228
229 togglePushButton = new QPushButton(tr(s: "Toggle Push Button"));
230 togglePushButton->setCheckable(true);
231 togglePushButton->setChecked(true);
232
233 flatPushButton = new QPushButton(tr(s: "Flat Push Button"));
234 flatPushButton->setFlat(true);
235
236 QVBoxLayout *layout = new QVBoxLayout;
237 layout->addWidget(defaultPushButton);
238 layout->addWidget(togglePushButton);
239 layout->addWidget(flatPushButton);
240 layout->addStretch(stretch: 1);
241 topRightGroupBox->setLayout(layout);
242}
243
244void WidgetGallery::createBottomLeftTabWidget()
245{
246 bottomLeftTabWidget = new QTabWidget;
247 bottomLeftTabWidget->setSizePolicy(hor: QSizePolicy::Preferred,
248 ver: QSizePolicy::Ignored);
249
250 QWidget *tab1 = new QWidget;
251 tableWidget = new QTableWidget(10, 10);
252
253 QHBoxLayout *tab1hbox = new QHBoxLayout;
254 tab1hbox->setContentsMargins(left: 5,top: 5, right: 5, bottom: 5);
255 tab1hbox->addWidget(tableWidget);
256 tab1->setLayout(tab1hbox);
257
258 QWidget *tab2 = new QWidget;
259 textEdit = new QTextEdit;
260
261 textEdit->setPlainText(tr(s: "Twinkle, twinkle, little star,\n"
262 "How I wonder what you are.\n"
263 "Up above the world so high,\n"
264 "Like a diamond in the sky.\n"
265 "Twinkle, twinkle, little star,\n"
266 "How I wonder what you are!\n"));
267
268 QHBoxLayout *tab2hbox = new QHBoxLayout;
269 tab2hbox->setContentsMargins(left: 5, top: 5, right: 5, bottom: 5);
270 tab2hbox->addWidget(textEdit);
271 tab2->setLayout(tab2hbox);
272
273 bottomLeftTabWidget->addTab(widget: tab1, tr(s: "&Table"));
274 bottomLeftTabWidget->addTab(widget: tab2, tr(s: "Text &Edit"));
275}
276
277void WidgetGallery::createBottomRightGroupBox()
278{
279 bottomRightGroupBox = new QGroupBox(tr(s: "Group 3"));
280 bottomRightGroupBox->setCheckable(true);
281 bottomRightGroupBox->setChecked(true);
282
283 lineEdit = new QLineEdit("s3cRe7");
284 lineEdit->setEchoMode(QLineEdit::Password);
285
286 spinBox = new QSpinBox(bottomRightGroupBox);
287 spinBox->setValue(50);
288
289 dateTimeEdit = new QDateTimeEdit(bottomRightGroupBox);
290 dateTimeEdit->setDateTime(QDateTime::currentDateTime());
291
292 slider = new QSlider(Qt::Horizontal, bottomRightGroupBox);
293 slider->setValue(40);
294
295 scrollBar = new QScrollBar(Qt::Horizontal, bottomRightGroupBox);
296 scrollBar->setValue(60);
297
298 dial = new QDial(bottomRightGroupBox);
299 dial->setValue(30);
300 dial->setNotchesVisible(true);
301
302 QGridLayout *layout = new QGridLayout;
303 layout->addWidget(lineEdit, row: 0, column: 0, rowSpan: 1, columnSpan: 2);
304 layout->addWidget(spinBox, row: 1, column: 0, rowSpan: 1, columnSpan: 2);
305 layout->addWidget(dateTimeEdit, row: 2, column: 0, rowSpan: 1, columnSpan: 2);
306 layout->addWidget(slider, row: 3, column: 0);
307 layout->addWidget(scrollBar, row: 4, column: 0);
308 layout->addWidget(dial, row: 3, column: 1, rowSpan: 2, columnSpan: 1);
309 layout->setRowStretch(row: 5, stretch: 1);
310 bottomRightGroupBox->setLayout(layout);
311}
312
313//! [13]
314void WidgetGallery::createProgressBar()
315{
316 progressBar = new QProgressBar;
317 progressBar->setRange(minimum: 0, maximum: 10000);
318 progressBar->setValue(0);
319
320 QTimer *timer = new QTimer(this);
321 connect(sender: timer, signal: &QTimer::timeout, receiver: this, slot: &WidgetGallery::advanceProgressBar);
322 timer->start(msec: 1000);
323}
324//! [13]
325

source code of qtbase/examples/widgets/widgets/styles/widgetgallery.cpp