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 <QComboBox>
55#include <QDateTimeEdit>
56#include <QGroupBox>
57#include <QHBoxLayout>
58#include <QLabel>
59#include <QSpinBox>
60
61//! [0]
62Window::Window(QWidget *parent)
63 : QWidget(parent)
64{
65 createSpinBoxes();
66 createDateTimeEdits();
67 createDoubleSpinBoxes();
68
69 QHBoxLayout *layout = new QHBoxLayout;
70 layout->addWidget(spinBoxesGroup);
71 layout->addWidget(editsGroup);
72 layout->addWidget(doubleSpinBoxesGroup);
73 setLayout(layout);
74
75 setWindowTitle(tr(s: "Spin Boxes"));
76}
77//! [0]
78
79//! [1]
80void Window::createSpinBoxes()
81{
82 spinBoxesGroup = new QGroupBox(tr(s: "Spinboxes"));
83
84 QLabel *integerLabel = new QLabel(tr(s: "Enter a value between "
85 "%1 and %2:").arg(a: -20).arg(a: 20));
86 QSpinBox *integerSpinBox = new QSpinBox;
87 integerSpinBox->setRange(min: -20, max: 20);
88 integerSpinBox->setSingleStep(1);
89 integerSpinBox->setValue(0);
90//! [1]
91
92//! [2]
93 QLabel *zoomLabel = new QLabel(tr(s: "Enter a zoom value between "
94 "%1 and %2:").arg(a: 0).arg(a: 1000));
95//! [3]
96 QSpinBox *zoomSpinBox = new QSpinBox;
97 zoomSpinBox->setRange(min: 0, max: 1000);
98 zoomSpinBox->setSingleStep(10);
99 zoomSpinBox->setSuffix("%");
100 zoomSpinBox->setSpecialValueText(tr(s: "Automatic"));
101 zoomSpinBox->setValue(100);
102//! [2] //! [3]
103
104//! [4]
105 QLabel *priceLabel = new QLabel(tr(s: "Enter a price between "
106 "%1 and %2:").arg(a: 0).arg(a: 999));
107 QSpinBox *priceSpinBox = new QSpinBox;
108 priceSpinBox->setRange(min: 0, max: 999);
109 priceSpinBox->setSingleStep(1);
110 priceSpinBox->setPrefix("$");
111 priceSpinBox->setValue(99);
112//! [4] //! [5]
113
114 groupSeparatorSpinBox = new QSpinBox;
115 groupSeparatorSpinBox->setRange(min: -99999999, max: 99999999);
116 groupSeparatorSpinBox->setValue(1000);
117 groupSeparatorSpinBox->setGroupSeparatorShown(true);
118 QCheckBox *groupSeparatorChkBox = new QCheckBox;
119 groupSeparatorChkBox->setText(tr(s: "Show group separator"));
120 groupSeparatorChkBox->setChecked(true);
121 connect(sender: groupSeparatorChkBox, signal: &QCheckBox::toggled, receiver: groupSeparatorSpinBox,
122 slot: &QSpinBox::setGroupSeparatorShown);
123
124 QLabel *hexLabel = new QLabel(tr(s: "Enter a value between "
125 "%1 and %2:").arg(a: '-' + QString::number(31, base: 16)).arg(a: QString::number(31, base: 16)));
126 QSpinBox *hexSpinBox = new QSpinBox;
127 hexSpinBox->setRange(min: -31, max: 31);
128 hexSpinBox->setSingleStep(1);
129 hexSpinBox->setValue(0);
130 hexSpinBox->setDisplayIntegerBase(16);
131
132 QVBoxLayout *spinBoxLayout = new QVBoxLayout;
133 spinBoxLayout->addWidget(integerLabel);
134 spinBoxLayout->addWidget(integerSpinBox);
135 spinBoxLayout->addWidget(zoomLabel);
136 spinBoxLayout->addWidget(zoomSpinBox);
137 spinBoxLayout->addWidget(priceLabel);
138 spinBoxLayout->addWidget(priceSpinBox);
139 spinBoxLayout->addWidget(hexLabel);
140 spinBoxLayout->addWidget(hexSpinBox);
141 spinBoxLayout->addWidget(groupSeparatorChkBox);
142 spinBoxLayout->addWidget(groupSeparatorSpinBox);
143 spinBoxesGroup->setLayout(spinBoxLayout);
144}
145//! [5]
146
147//! [6]
148void Window::createDateTimeEdits()
149{
150 editsGroup = new QGroupBox(tr(s: "Date and time spin boxes"));
151
152 QLabel *dateLabel = new QLabel;
153 QDateEdit *dateEdit = new QDateEdit(QDate::currentDate());
154 dateEdit->setDateRange(min: QDate(2005, 1, 1), max: QDate(2010, 12, 31));
155 dateLabel->setText(tr(s: "Appointment date (between %0 and %1):")
156 .arg(a: dateEdit->minimumDate().toString(format: Qt::ISODate))
157 .arg(a: dateEdit->maximumDate().toString(format: Qt::ISODate)));
158//! [6]
159
160//! [7]
161 QLabel *timeLabel = new QLabel;
162 QTimeEdit *timeEdit = new QTimeEdit(QTime::currentTime());
163 timeEdit->setTimeRange(min: QTime(9, 0, 0, 0), max: QTime(16, 30, 0, 0));
164 timeLabel->setText(tr(s: "Appointment time (between %0 and %1):")
165 .arg(a: timeEdit->minimumTime().toString(f: Qt::ISODate))
166 .arg(a: timeEdit->maximumTime().toString(f: Qt::ISODate)));
167//! [7]
168
169//! [8]
170 meetingLabel = new QLabel;
171 meetingEdit = new QDateTimeEdit(QDateTime::currentDateTime());
172//! [8]
173
174//! [9]
175 QLabel *formatLabel = new QLabel(tr(s: "Format string for the meeting date "
176 "and time:"));
177 QComboBox *formatComboBox = new QComboBox;
178 formatComboBox->addItem(atext: "yyyy-MM-dd hh:mm:ss (zzz 'ms')");
179 formatComboBox->addItem(atext: "hh:mm:ss MM/dd/yyyy");
180 formatComboBox->addItem(atext: "hh:mm:ss dd/MM/yyyy");
181 formatComboBox->addItem(atext: "hh:mm:ss");
182 formatComboBox->addItem(atext: "hh:mm ap");
183//! [9] //! [10]
184
185 connect(sender: formatComboBox, signal: &QComboBox::textActivated,
186 receiver: this, slot: &Window::setFormatString);
187//! [10]
188
189 setFormatString(formatComboBox->currentText());
190
191//! [11]
192 QVBoxLayout *editsLayout = new QVBoxLayout;
193 editsLayout->addWidget(dateLabel);
194 editsLayout->addWidget(dateEdit);
195 editsLayout->addWidget(timeLabel);
196 editsLayout->addWidget(timeEdit);
197 editsLayout->addWidget(meetingLabel);
198 editsLayout->addWidget(meetingEdit);
199 editsLayout->addWidget(formatLabel);
200 editsLayout->addWidget(formatComboBox);
201 editsGroup->setLayout(editsLayout);
202}
203//! [11]
204
205//! [12]
206void Window::setFormatString(const QString &formatString)
207{
208 meetingEdit->setDisplayFormat(formatString);
209//! [12] //! [13]
210 if (meetingEdit->displayedSections() & QDateTimeEdit::DateSections_Mask) {
211 meetingEdit->setDateRange(min: QDate(2004, 11, 1), max: QDate(2005, 11, 30));
212 meetingLabel->setText(tr(s: "Meeting date (between %0 and %1):")
213 .arg(a: meetingEdit->minimumDate().toString(format: Qt::ISODate))
214 .arg(a: meetingEdit->maximumDate().toString(format: Qt::ISODate)));
215 } else {
216 meetingEdit->setTimeRange(min: QTime(0, 7, 20, 0), max: QTime(21, 0, 0, 0));
217 meetingLabel->setText(tr(s: "Meeting time (between %0 and %1):")
218 .arg(a: meetingEdit->minimumTime().toString(f: Qt::ISODate))
219 .arg(a: meetingEdit->maximumTime().toString(f: Qt::ISODate)));
220 }
221}
222//! [13]
223
224//! [14]
225void Window::createDoubleSpinBoxes()
226{
227 doubleSpinBoxesGroup = new QGroupBox(tr(s: "Double precision spinboxes"));
228
229 QLabel *precisionLabel = new QLabel(tr(s: "Number of decimal places "
230 "to show:"));
231 QSpinBox *precisionSpinBox = new QSpinBox;
232 precisionSpinBox->setRange(min: 0, max: 100);
233 precisionSpinBox->setValue(2);
234//! [14]
235
236//! [15]
237 QLabel *doubleLabel = new QLabel(tr(s: "Enter a value between "
238 "%1 and %2:").arg(a: -20).arg(a: 20));
239 doubleSpinBox = new QDoubleSpinBox;
240 doubleSpinBox->setRange(min: -20.0, max: 20.0);
241 doubleSpinBox->setSingleStep(1.0);
242 doubleSpinBox->setValue(0.0);
243//! [15]
244
245//! [16]
246 QLabel *scaleLabel = new QLabel(tr(s: "Enter a scale factor between "
247 "%1 and %2:").arg(a: 0).arg(a: 1000.0));
248 scaleSpinBox = new QDoubleSpinBox;
249 scaleSpinBox->setRange(min: 0.0, max: 1000.0);
250 scaleSpinBox->setSingleStep(10.0);
251 scaleSpinBox->setSuffix("%");
252 scaleSpinBox->setSpecialValueText(tr(s: "No scaling"));
253 scaleSpinBox->setValue(100.0);
254//! [16]
255
256//! [17]
257 QLabel *priceLabel = new QLabel(tr(s: "Enter a price between "
258 "%1 and %2:").arg(a: 0).arg(a: 1000));
259 priceSpinBox = new QDoubleSpinBox;
260 priceSpinBox->setRange(min: 0.0, max: 1000.0);
261 priceSpinBox->setSingleStep(1.0);
262 priceSpinBox->setPrefix("$");
263 priceSpinBox->setValue(99.99);
264
265 connect(sender: precisionSpinBox, signal: QOverload<int>::of(ptr: &QSpinBox::valueChanged),
266//! [17]
267 receiver: this, slot: &Window::changePrecision);
268
269 groupSeparatorSpinBox_d = new QDoubleSpinBox;
270 groupSeparatorSpinBox_d->setRange(min: -99999999, max: 99999999);
271 groupSeparatorSpinBox_d->setDecimals(2);
272 groupSeparatorSpinBox_d->setValue(1000.00);
273 groupSeparatorSpinBox_d->setGroupSeparatorShown(true);
274 QCheckBox *groupSeparatorChkBox = new QCheckBox;
275 groupSeparatorChkBox->setText(tr(s: "Show group separator"));
276 groupSeparatorChkBox->setChecked(true);
277 connect(sender: groupSeparatorChkBox, signal: &QCheckBox::toggled, receiver: groupSeparatorSpinBox_d,
278 slot: &QDoubleSpinBox::setGroupSeparatorShown);
279
280//! [18]
281 QVBoxLayout *spinBoxLayout = new QVBoxLayout;
282 spinBoxLayout->addWidget(precisionLabel);
283 spinBoxLayout->addWidget(precisionSpinBox);
284 spinBoxLayout->addWidget(doubleLabel);
285 spinBoxLayout->addWidget(doubleSpinBox);
286 spinBoxLayout->addWidget(scaleLabel);
287 spinBoxLayout->addWidget(scaleSpinBox);
288 spinBoxLayout->addWidget(priceLabel);
289 spinBoxLayout->addWidget(priceSpinBox);
290 spinBoxLayout->addWidget(groupSeparatorChkBox);
291 spinBoxLayout->addWidget(groupSeparatorSpinBox_d);
292 doubleSpinBoxesGroup->setLayout(spinBoxLayout);
293}
294//! [18]
295
296//! [19]
297void Window::changePrecision(int decimals)
298{
299 doubleSpinBox->setDecimals(decimals);
300 scaleSpinBox->setDecimals(decimals);
301 priceSpinBox->setDecimals(decimals);
302}
303//! [19]
304

source code of qtbase/examples/widgets/widgets/spinboxes/window.cpp