2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-22 12:24:00 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the documentation of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:FDL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** 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
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-22 12:24:00 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
|
|
|
** GNU Free Documentation License Usage
|
2011-04-27 10:05:43 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Free
|
|
|
|
** Documentation License version 1.3 as published by the Free Software
|
2011-05-24 09:34:08 +00:00
|
|
|
** Foundation and appearing in the file included in the packaging of
|
2015-01-28 08:44:43 +00:00
|
|
|
** this file. Please review the following information to ensure
|
2012-09-19 12:28:29 +00:00
|
|
|
** the GNU Free Documentation License version 1.3 requirements
|
2016-01-22 12:24:00 +00:00
|
|
|
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-03-14 23:42:15 +00:00
|
|
|
/*!
|
2012-08-29 08:25:56 +00:00
|
|
|
\example widgets/spinboxes
|
2011-04-27 10:05:43 +00:00
|
|
|
\title Spin Boxes Example
|
2012-10-22 11:39:07 +00:00
|
|
|
\ingroup examples-widgets
|
|
|
|
\brief The Spin Boxes example shows how to use the many different types of
|
|
|
|
spin boxes available in Qt, from a simple QSpinBox widget to more complex
|
|
|
|
editors like the QDateTimeEdit widget.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2016-11-16 16:33:12 +00:00
|
|
|
\borderedimage spinboxes-example.png
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The example consists of a single \c Window class that is used to display the
|
|
|
|
different spin box-based widgets available with Qt.
|
|
|
|
|
|
|
|
\section1 Window Class Definition
|
|
|
|
|
|
|
|
The \c Window class inherits QWidget and contains two slots that are used
|
|
|
|
to provide interactive features:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.h 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The private functions are used to set up each type of spin box in the window.
|
|
|
|
We use member variables to keep track of various widgets so that they can
|
|
|
|
be reconfigured when required.
|
|
|
|
|
|
|
|
\section1 Window Class Implementation
|
|
|
|
|
|
|
|
The constructor simply calls private functions to set up the different types
|
|
|
|
of spin box used in the example, and places each group in a layout:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
We use the layout to manage the arrangement of the window's child widgets,
|
|
|
|
and change the window title.
|
|
|
|
|
|
|
|
The \c createSpinBoxes() function constructs a QGroupBox and places three
|
|
|
|
QSpinBox widgets inside it with descriptive labels to indicate the types of
|
|
|
|
input they expect.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 1
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The first spin box shows the simplest way to use QSpinBox. It accepts values
|
|
|
|
from -20 to 20, the current value can be increased or decreased by 1 with
|
2012-08-01 12:36:13 +00:00
|
|
|
either the arrow buttons or \uicontrol{Up} and \uicontrol{Down} keys, and the default
|
2011-04-27 10:05:43 +00:00
|
|
|
value is 0.
|
|
|
|
|
|
|
|
The second spin box uses a larger step size and displays a suffix to
|
|
|
|
provide more information about the type of data the number represents:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 2
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
This spin box also displays a
|
|
|
|
\l{QAbstractSpinBox::specialValueText}{special value} instead of the minimum
|
2012-08-01 12:36:13 +00:00
|
|
|
value defined for it. This means that it will never show \uicontrol{0%}, but will
|
|
|
|
display \uicontrol{Automatic} when the minimum value is selected.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The third spin box shows how a prefix can be used:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 4
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
For simplicity, we show a spin box with a prefix and no suffix. It is also
|
|
|
|
possible to use both at the same time.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 5
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The rest of the function sets up a layout for the group box and places each
|
|
|
|
of the widgets inside it.
|
|
|
|
|
|
|
|
The \c createDateTimeEdits() function constructs another group box with a
|
|
|
|
selection of spin boxes used for editing dates and times.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 6
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The first spin box is a QDateEdit widget that is able to accept dates
|
|
|
|
within a given range specified using QDate values. The arrow buttons and
|
2012-08-01 12:36:13 +00:00
|
|
|
\uicontrol{Up} and \uicontrol{Down} keys can be used to increase and decrease the
|
2011-04-27 10:05:43 +00:00
|
|
|
values for year, month, and day when the cursor is in the relevant section.
|
|
|
|
|
|
|
|
The second spin box is a QTimeEdit widget:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 7
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Acceptable values for the time are defined using QTime values.
|
|
|
|
|
|
|
|
The third spin box is a QDateTimeEdit widget that can display both date and
|
|
|
|
time values, and we place a label above it to indicate the range of allowed
|
|
|
|
times for a meeting. These widgets will be updated when the user changes a
|
|
|
|
format string.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 8
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The format string used for the date time editor, which is also shown in the
|
|
|
|
string displayed by the label, is chosen from a set of strings in a combobox:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 9
|
2011-04-27 10:05:43 +00:00
|
|
|
\codeline
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 10
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
A signal from this combobox is connected to a slot in the \c Window class
|
|
|
|
(shown later).
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 11
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Each child widget of the group box in placed in a layout.
|
|
|
|
|
|
|
|
The \c setFormatString() slot is called whenever the user selects a new
|
|
|
|
format string in the combobox. The display format for the QDateTimeEdit
|
|
|
|
widget is set using the raw string passed by the signal:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 12
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Depending on the visible sections in the widget, we set a new date or time
|
|
|
|
range, and update the associated label to provide relevant information for
|
|
|
|
the user:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 13
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
When the format string is changed, there will be an appropriate label and
|
|
|
|
entry widget for dates, times, or both types of input.
|
|
|
|
|
|
|
|
The \c createDoubleSpinBoxes() function constructs three spin boxes that are
|
|
|
|
used to input double-precision floating point numbers:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 14
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
Before the QDoubleSpinBox widgets are constructed, we create a spin box to
|
|
|
|
control how many decimal places they show. By default, only two decimal places
|
|
|
|
are shown in the following spin boxes, each of which is the equivalent of a
|
|
|
|
spin box in the group created by the \c createSpinBoxes() function.
|
|
|
|
|
|
|
|
The first double spin box shows a basic double-precision spin box with the
|
|
|
|
same range, step size, and default value as the first spin box in the
|
|
|
|
\c createSpinBoxes() function:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 15
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
However, this spin box also allows non-integer values to be entered.
|
|
|
|
|
|
|
|
The second spin box displays a suffix and shows a special value instead
|
|
|
|
of the minimum value:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 16
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The third spin box displays a prefix instead of a suffix:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 17
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
We connect the QSpinBox widget that specifies the precision to a slot in
|
|
|
|
the \c Window class.
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 18
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
The rest of the function places each of the widgets into a layout for the
|
|
|
|
group box.
|
|
|
|
|
|
|
|
The \c changePrecision() slot is called when the user changes the value in
|
|
|
|
the precision spin box:
|
|
|
|
|
2012-08-29 08:25:56 +00:00
|
|
|
\snippet widgets/spinboxes/window.cpp 19
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
This function simply uses the integer supplied by the signal to specify the
|
|
|
|
number of decimal places in each of the QDoubleSpinBox widgets. Each one
|
|
|
|
of these will be updated automatically when their
|
|
|
|
\l{QDoubleSpinBox::decimals}{decimals} property is changed.
|
|
|
|
*/
|