1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: window.cpp Example File (widgets/spinboxes/window.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">window.cpp Example File<br /><small><small>widgets/spinboxes/window.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
50
#include "window.h"
55
createDateTimeEdits();
56
createDoubleSpinBoxes();
58
QHBoxLayout *layout = new QHBoxLayout;
59
layout->addWidget(spinBoxesGroup);
60
layout->addWidget(editsGroup);
61
layout->addWidget(doubleSpinBoxesGroup);
64
setWindowTitle(tr("Spin Boxes"));
67
void Window::createSpinBoxes()
69
spinBoxesGroup = new QGroupBox(tr("Spinboxes"));
71
QLabel *integerLabel = new QLabel(tr("Enter a value between "
72
"%1 and %2:").arg(-20).arg(20));
73
QSpinBox *integerSpinBox = new QSpinBox;
74
integerSpinBox->setRange(-20, 20);
75
integerSpinBox->setSingleStep(1);
76
integerSpinBox->setValue(0);
78
QLabel *zoomLabel = new QLabel(tr("Enter a zoom value between "
79
"%1 and %2:").arg(0).arg(1000));
80
QSpinBox *zoomSpinBox = new QSpinBox;
81
zoomSpinBox->setRange(0, 1000);
82
zoomSpinBox->setSingleStep(10);
83
zoomSpinBox->setSuffix("%");
84
zoomSpinBox->setSpecialValueText(tr("Automatic"));
85
zoomSpinBox->setValue(100);
87
QLabel *priceLabel = new QLabel(tr("Enter a price between "
88
"%1 and %2:").arg(0).arg(999));
89
QSpinBox *priceSpinBox = new QSpinBox;
90
priceSpinBox->setRange(0, 999);
91
priceSpinBox->setSingleStep(1);
92
priceSpinBox->setPrefix("$");
93
priceSpinBox->setValue(99);
95
QVBoxLayout *spinBoxLayout = new QVBoxLayout;
96
spinBoxLayout->addWidget(integerLabel);
97
spinBoxLayout->addWidget(integerSpinBox);
98
spinBoxLayout->addWidget(zoomLabel);
99
spinBoxLayout->addWidget(zoomSpinBox);
100
spinBoxLayout->addWidget(priceLabel);
101
spinBoxLayout->addWidget(priceSpinBox);
102
spinBoxesGroup->setLayout(spinBoxLayout);
105
void Window::createDateTimeEdits()
107
editsGroup = new QGroupBox(tr("Date and time spin boxes"));
109
QLabel *dateLabel = new QLabel;
110
QDateTimeEdit *dateEdit = new QDateTimeEdit(QDate::currentDate());
111
dateEdit->setDateRange(QDate(2005, 1, 1), QDate(2010, 12, 31));
112
dateLabel->setText(tr("Appointment date (between %0 and %1):")
113
.arg(dateEdit->minimumDate().toString(Qt::ISODate))
114
.arg(dateEdit->maximumDate().toString(Qt::ISODate)));
116
QLabel *timeLabel = new QLabel;
117
QDateTimeEdit *timeEdit = new QDateTimeEdit(QTime::currentTime());
118
timeEdit->setTimeRange(QTime(9, 0, 0, 0), QTime(16, 30, 0, 0));
119
timeLabel->setText(tr("Appointment time (between %0 and %1):")
120
.arg(timeEdit->minimumTime().toString(Qt::ISODate))
121
.arg(timeEdit->maximumTime().toString(Qt::ISODate)));
123
meetingLabel = new QLabel;
124
meetingEdit = new QDateTimeEdit(QDateTime::currentDateTime());
126
QLabel *formatLabel = new QLabel(tr("Format string for the meeting date "
127
"and time:"));
128
QComboBox *formatComboBox = new QComboBox;
129
formatComboBox->addItem("yyyy-MM-dd hh:mm:ss (zzz ms)");
130
formatComboBox->addItem("hh:mm:ss MM/dd/yyyy");
131
formatComboBox->addItem("hh:mm:ss dd/MM/yyyy");
132
formatComboBox->addItem("hh:mm:ss");
133
formatComboBox->addItem("hh:mm ap");
135
connect(formatComboBox, SIGNAL(activated(const QString &)),
136
this, SLOT(setFormatString(const QString &)));
138
setFormatString(formatComboBox->currentText());
140
QVBoxLayout *editsLayout = new QVBoxLayout;
141
editsLayout->addWidget(dateLabel);
142
editsLayout->addWidget(dateEdit);
143
editsLayout->addWidget(timeLabel);
144
editsLayout->addWidget(timeEdit);
145
editsLayout->addWidget(meetingLabel);
146
editsLayout->addWidget(meetingEdit);
147
editsLayout->addWidget(formatLabel);
148
editsLayout->addWidget(formatComboBox);
149
editsGroup->setLayout(editsLayout);
152
void Window::setFormatString(const QString &formatString)
154
meetingEdit->setDisplayFormat(formatString);
155
if (meetingEdit->displayedSections() & QDateTimeEdit::DateSections_Mask) {
156
meetingEdit->setDateRange(QDate(2004, 11, 1), QDate(2005, 11, 30));
157
meetingLabel->setText(tr("Meeting date (between %0 and %1):")
158
.arg(meetingEdit->minimumDate().toString(Qt::ISODate))
159
.arg(meetingEdit->maximumDate().toString(Qt::ISODate)));
161
meetingEdit->setTimeRange(QTime(0, 7, 20, 0), QTime(21, 0, 0, 0));
162
meetingLabel->setText(tr("Meeting time (between %0 and %1):")
163
.arg(meetingEdit->minimumTime().toString(Qt::ISODate))
164
.arg(meetingEdit->maximumTime().toString(Qt::ISODate)));
168
void Window::createDoubleSpinBoxes()
170
doubleSpinBoxesGroup = new QGroupBox(tr("Double precision spinboxes"));
172
QLabel *precisionLabel = new QLabel(tr("Number of decimal places "
173
"to show:"));
174
QSpinBox *precisionSpinBox = new QSpinBox;
175
precisionSpinBox->setRange(0, 14);
176
precisionSpinBox->setValue(2);
178
QLabel *doubleLabel = new QLabel(tr("Enter a value between "
179
"%1 and %2:").arg(-20).arg(20));
180
doubleSpinBox = new QDoubleSpinBox;
181
doubleSpinBox->setRange(-20.0, 20.0);
182
doubleSpinBox->setSingleStep(1.0);
183
doubleSpinBox->setValue(0.0);
185
QLabel *scaleLabel = new QLabel(tr("Enter a scale factor between "
186
"%1 and %2:").arg(0).arg(1000.0));
187
scaleSpinBox = new QDoubleSpinBox;
188
scaleSpinBox->setRange(0.0, 1000.0);
189
scaleSpinBox->setSingleStep(10.0);
190
scaleSpinBox->setSuffix("%");
191
scaleSpinBox->setSpecialValueText(tr("No scaling"));
192
scaleSpinBox->setValue(100.0);
194
QLabel *priceLabel = new QLabel(tr("Enter a price between "
195
"%1 and %2:").arg(0).arg(1000));
196
priceSpinBox = new QDoubleSpinBox;
197
priceSpinBox->setRange(0.0, 1000.0);
198
priceSpinBox->setSingleStep(1.0);
199
priceSpinBox->setPrefix("$");
200
priceSpinBox->setValue(99.99);
202
connect(precisionSpinBox, SIGNAL(valueChanged(int)),
203
this, SLOT(changePrecision(int)));
205
QVBoxLayout *spinBoxLayout = new QVBoxLayout;
206
spinBoxLayout->addWidget(precisionLabel);
207
spinBoxLayout->addWidget(precisionSpinBox);
208
spinBoxLayout->addWidget(doubleLabel);
209
spinBoxLayout->addWidget(doubleSpinBox);
210
spinBoxLayout->addWidget(scaleLabel);
211
spinBoxLayout->addWidget(scaleSpinBox);
212
spinBoxLayout->addWidget(priceLabel);
213
spinBoxLayout->addWidget(priceSpinBox);
214
doubleSpinBoxesGroup->setLayout(spinBoxLayout);
217
void Window::changePrecision(int decimals)
219
doubleSpinBox->setDecimals(decimals);
220
scaleSpinBox->setDecimals(decimals);
221
priceSpinBox->setDecimals(decimals);
223
<p /><address><hr /><div align="center">
224
<table width="100%" cellspacing="0" border="0"><tr class="address">
225
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
226
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
227
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
228
</tr></table></div></address></body>