Spin Boxes Example▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include <QtWidgets>
#include
"window.h"
Window::
Window()
{
createSpinBoxes();
createDateTimeEdits();
createDoubleSpinBoxes();
QHBoxLayout *
layout =
new
QHBoxLayout;
layout-&
gt;addWidget(spinBoxesGroup);
layout-&
gt;addWidget(editsGroup);
layout-&
gt;addWidget(doubleSpinBoxesGroup);
setLayout(layout);
setWindowTitle(tr("Spin Boxes"
));
}
void
Window::
createSpinBoxes()
{
spinBoxesGroup =
new
QGroupBox(tr("Spinboxes"
));
QLabel *
integerLabel =
new
QLabel(tr("Enter a value between "
"%1 and %2:"
).arg(-
20
).arg(20
));
QSpinBox *
integerSpinBox =
new
QSpinBox;
integerSpinBox-&
gt;setRange(-
20
, 20
);
integerSpinBox-&
gt;setSingleStep(1
);
integerSpinBox-&
gt;setValue(0
);
QLabel *
zoomLabel =
new
QLabel(tr("Enter a zoom value between "
"%1 and %2:"
).arg(0
).arg(1000
));
QSpinBox *
zoomSpinBox =
new
QSpinBox;
zoomSpinBox-&
gt;setRange(0
, 1000
);
zoomSpinBox-&
gt;setSingleStep(10
);
zoomSpinBox-&
gt;setSuffix("%"
);
zoomSpinBox-&
gt;setSpecialValueText(tr("Automatic"
));
zoomSpinBox-&
gt;setValue(100
);
QLabel *
priceLabel =
new
QLabel(tr("Enter a price between "
"%1 and %2:"
).arg(0
).arg(999
));
QSpinBox *
priceSpinBox =
new
QSpinBox;
priceSpinBox-&
gt;setRange(0
, 999
);
priceSpinBox-&
gt;setSingleStep(1
);
priceSpinBox-&
gt;setPrefix("$"
);
priceSpinBox-&
gt;setValue(99
);
groupSeparatorSpinBox =
new
QSpinBox;
groupSeparatorSpinBox-&
gt;setRange(-
99999999
, 99999999
);
groupSeparatorSpinBox-&
gt;setValue(1000
);
groupSeparatorSpinBox-&
gt;setGroupSeparatorShown(true
);
QCheckBox *
groupSeparatorChkBox =
new
QCheckBox;
groupSeparatorChkBox-&
gt;setText(tr("Show group separator"
));
groupSeparatorChkBox-&
gt;setChecked(true
);
connect(groupSeparatorChkBox, &
amp;QCheckBox::
toggled, groupSeparatorSpinBox,
&
amp;QSpinBox::
setGroupSeparatorShown);
QLabel *
hexLabel =
new
QLabel(tr("Enter a value between "
"%1 and %2:"
).arg('-'
+
QString::
number(31
, 16
)).arg(QString::
number(31
, 16
)));
QSpinBox *
hexSpinBox =
new
QSpinBox;
hexSpinBox-&
gt;setRange(-
31
, 31
);
hexSpinBox-&
gt;setSingleStep(1
);
hexSpinBox-&
gt;setValue(0
);
hexSpinBox-&
gt;setDisplayIntegerBase(16
);
QVBoxLayout *
spinBoxLayout =
new
QVBoxLayout;
spinBoxLayout-&
gt;addWidget(integerLabel);
spinBoxLayout-&
gt;addWidget(integerSpinBox);
spinBoxLayout-&
gt;addWidget(zoomLabel);
spinBoxLayout-&
gt;addWidget(zoomSpinBox);
spinBoxLayout-&
gt;addWidget(priceLabel);
spinBoxLayout-&
gt;addWidget(priceSpinBox);
spinBoxLayout-&
gt;addWidget(hexLabel);
spinBoxLayout-&
gt;addWidget(hexSpinBox);
spinBoxLayout-&
gt;addWidget(groupSeparatorChkBox);
spinBoxLayout-&
gt;addWidget(groupSeparatorSpinBox);
spinBoxesGroup-&
gt;setLayout(spinBoxLayout);
}
void
Window::
createDateTimeEdits()
{
editsGroup =
new
QGroupBox(tr("Date and time spin boxes"
));
QLabel *
dateLabel =
new
QLabel;
QDateEdit *
dateEdit =
new
QDateEdit(QDate::
currentDate());
dateEdit-&
gt;setDateRange(QDate(2005
, 1
, 1
), QDate(2010
, 12
, 31
));
dateLabel-&
gt;setText(tr("Appointment date (between %0 and %1):"
)
.arg(dateEdit-&
gt;minimumDate().toString(Qt::
ISODate))
.arg(dateEdit-&
gt;maximumDate().toString(Qt::
ISODate)));
QLabel *
timeLabel =
new
QLabel;
QTimeEdit *
timeEdit =
new
QTimeEdit(QTime::
currentTime());
timeEdit-&
gt;setTimeRange(QTime(9
, 0
, 0
, 0
), QTime(16
, 30
, 0
, 0
));
timeLabel-&
gt;setText(tr("Appointment time (between %0 and %1):"
)
.arg(timeEdit-&
gt;minimumTime().toString(Qt::
ISODate))
.arg(timeEdit-&
gt;maximumTime().toString(Qt::
ISODate)));
meetingLabel =
new
QLabel;
meetingEdit =
new
QDateTimeEdit(QDateTime::
currentDateTime());
QLabel *
formatLabel =
new
QLabel(tr("Format string for the meeting date "
"and time:"
));
QComboBox *
formatComboBox =
new
QComboBox;
formatComboBox-&
gt;addItem("yyyy-MM-dd hh:mm:ss (zzz 'ms')"
);
formatComboBox-&
gt;addItem("hh:mm:ss MM/dd/yyyy"
);
formatComboBox-&
gt;addItem("hh:mm:ss dd/MM/yyyy"
);
formatComboBox-&
gt;addItem("hh:mm:ss"
);
formatComboBox-&
gt;addItem("hh:mm ap"
);
connect(formatComboBox, SIGNAL(activated(QString)),
this
, SLOT(setFormatString(QString)));
setFormatString(formatComboBox-&
gt;currentText());
QVBoxLayout *
editsLayout =
new
QVBoxLayout;
editsLayout-&
gt;addWidget(dateLabel);
editsLayout-&
gt;addWidget(dateEdit);
editsLayout-&
gt;addWidget(timeLabel);
editsLayout-&
gt;addWidget(timeEdit);
editsLayout-&
gt;addWidget(meetingLabel);
editsLayout-&
gt;addWidget(meetingEdit);
editsLayout-&
gt;addWidget(formatLabel);
editsLayout-&
gt;addWidget(formatComboBox);
editsGroup-&
gt;setLayout(editsLayout);
}
void
Window::
setFormatString(const
QString &
amp;formatString)
{
meetingEdit-&
gt;setDisplayFormat(formatString);
if
(meetingEdit-&
gt;displayedSections() &
amp; QDateTimeEdit::
DateSections_Mask) {
meetingEdit-&
gt;setDateRange(QDate(2004
, 11
, 1
), QDate(2005
, 11
, 30
));
meetingLabel-&
gt;setText(tr("Meeting date (between %0 and %1):"
)
.arg(meetingEdit-&
gt;minimumDate().toString(Qt::
ISODate))
.arg(meetingEdit-&
gt;maximumDate().toString(Qt::
ISODate)));
}
else
{
meetingEdit-&
gt;setTimeRange(QTime(0
, 7
, 20
, 0
), QTime(21
, 0
, 0
, 0
));
meetingLabel-&
gt;setText(tr("Meeting time (between %0 and %1):"
)
.arg(meetingEdit-&
gt;minimumTime().toString(Qt::
ISODate))
.arg(meetingEdit-&
gt;maximumTime().toString(Qt::
ISODate)));
}
}
void
Window::
createDoubleSpinBoxes()
{
doubleSpinBoxesGroup =
new
QGroupBox(tr("Double precision spinboxes"
));
QLabel *
precisionLabel =
new
QLabel(tr("Number of decimal places "
"to show:"
));
QSpinBox *
precisionSpinBox =
new
QSpinBox;
precisionSpinBox-&
gt;setRange(0
, 100
);
precisionSpinBox-&
gt;setValue(2
);
QLabel *
doubleLabel =
new
QLabel(tr("Enter a value between "
"%1 and %2:"
).arg(-
20
).arg(20
));
doubleSpinBox =
new
QDoubleSpinBox;
doubleSpinBox-&
gt;setRange(-
20.0
, 20.0
);
doubleSpinBox-&
gt;setSingleStep(1.0
);
doubleSpinBox-&
gt;setValue(0.0
);
QLabel *
scaleLabel =
new
QLabel(tr("Enter a scale factor between "
"%1 and %2:"
).arg(0
).arg(1000.0
));
scaleSpinBox =
new
QDoubleSpinBox;
scaleSpinBox-&
gt;setRange(0.0
, 1000.0
);
scaleSpinBox-&
gt;setSingleStep(10.0
);
scaleSpinBox-&
gt;setSuffix("%"
);
scaleSpinBox-&
gt;setSpecialValueText(tr("No scaling"
));
scaleSpinBox-&
gt;setValue(100.0
);
QLabel *
priceLabel =
new
QLabel(tr("Enter a price between "
"%1 and %2:"
).arg(0
).arg(1000
));
priceSpinBox =
new
QDoubleSpinBox;
priceSpinBox-&
gt;setRange(0.0
, 1000.0
);
priceSpinBox-&
gt;setSingleStep(1.0
);
priceSpinBox-&
gt;setPrefix("$"
);
priceSpinBox-&
gt;setValue(99.99
);
connect(precisionSpinBox, SIGNAL(valueChanged(int
)),
this
, SLOT(changePrecision(int
)));
groupSeparatorSpinBox_d =
new
QDoubleSpinBox;
groupSeparatorSpinBox_d-&
gt;setRange(-
99999999
, 99999999
);
groupSeparatorSpinBox_d-&
gt;setDecimals(2
);
groupSeparatorSpinBox_d-&
gt;setValue(1000.00
);
groupSeparatorSpinBox_d-&
gt;setGroupSeparatorShown(true
);
QCheckBox *
groupSeparatorChkBox =
new
QCheckBox;
groupSeparatorChkBox-&
gt;setText(tr("Show group separator"
));
groupSeparatorChkBox-&
gt;setChecked(true
);
connect(groupSeparatorChkBox, &
amp;QCheckBox::
toggled, groupSeparatorSpinBox_d,
&
amp;QDoubleSpinBox::
setGroupSeparatorShown);
QVBoxLayout *
spinBoxLayout =
new
QVBoxLayout;
spinBoxLayout-&
gt;addWidget(precisionLabel);
spinBoxLayout-&
gt;addWidget(precisionSpinBox);
spinBoxLayout-&
gt;addWidget(doubleLabel);
spinBoxLayout-&
gt;addWidget(doubleSpinBox);
spinBoxLayout-&
gt;addWidget(scaleLabel);
spinBoxLayout-&
gt;addWidget(scaleSpinBox);
spinBoxLayout-&
gt;addWidget(priceLabel);
spinBoxLayout-&
gt;addWidget(priceSpinBox);
spinBoxLayout-&
gt;addWidget(groupSeparatorChkBox);
spinBoxLayout-&
gt;addWidget(groupSeparatorSpinBox_d);
doubleSpinBoxesGroup-&
gt;setLayout(spinBoxLayout);
}
void
Window::
changePrecision(int
decimals)
{
doubleSpinBox-&
gt;setDecimals(decimals);
scaleSpinBox-&
gt;setDecimals(decimals);
priceSpinBox-&
gt;setDecimals(decimals);
}