[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 16 | ** GNU Free Documentation License
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
| 20 | ** file.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \example widgets/lineedits
|
---|
| 30 | \title Line Edits Example
|
---|
| 31 |
|
---|
| 32 | The Line Edits example demonstrates the many ways that QLineEdit can be used, and
|
---|
| 33 | shows the effects of various properties and validators on the input and output
|
---|
| 34 | supplied by the user.
|
---|
| 35 |
|
---|
| 36 | \image lineedits-example.png
|
---|
| 37 |
|
---|
| 38 | The example consists of a single \c Window class, containing a selection of
|
---|
| 39 | line edits with different input constraints and display properties that can be
|
---|
| 40 | changed by selecting items from comboboxes. Presenting these together helps
|
---|
| 41 | developers choose suitable properties to use with line edits, and makes it easy
|
---|
| 42 | to compare the effects of each validator on user input.
|
---|
| 43 |
|
---|
| 44 | \section1 Window Class Definition
|
---|
| 45 |
|
---|
| 46 | The \c Window class inherits QWidget and contains a constructor and several
|
---|
| 47 | slots:
|
---|
| 48 |
|
---|
| 49 | \snippet examples/widgets/lineedits/window.h 0
|
---|
| 50 |
|
---|
| 51 | The slots are used to update the type of validator used for a given line edit when
|
---|
| 52 | a new validator has been selected in the associated combobox. The line edits
|
---|
| 53 | are stored in the window for use in these slots.
|
---|
| 54 |
|
---|
| 55 | \section1 Window Class Implementation
|
---|
| 56 |
|
---|
| 57 | The \c Window constructor is used to set up the line edits, validators,
|
---|
| 58 | and comboboxes, connect signals from the comboboxes to slots in the \c Window
|
---|
| 59 | class, and arrange the child widgets in layouts.
|
---|
| 60 |
|
---|
| 61 | We begin by constructing a \l{QGroupBox}{group box} to hold a label, combobox,
|
---|
| 62 | and line edit so that we can demonstrate the QLineEdit::echoMode property:
|
---|
| 63 |
|
---|
| 64 | \snippet examples/widgets/lineedits/window.cpp 0
|
---|
| 65 |
|
---|
| 66 | At this point, none of these widgets have been arranged in layouts. Eventually,
|
---|
| 67 | the \c echoLabel, \c echoComboBox, and \c echoLineEdit will be placed in a
|
---|
| 68 | vertical layout inside the \c echoGroup group box.
|
---|
| 69 |
|
---|
| 70 | Similarly, we construct group boxes and collections of widgets to show the
|
---|
| 71 | effects of QIntValidator and QDoubleValidator on a line edit's contents:
|
---|
| 72 |
|
---|
| 73 | \snippet examples/widgets/lineedits/window.cpp 1
|
---|
| 74 |
|
---|
| 75 | Text alignment is demonstrated by another group of widgets:
|
---|
| 76 |
|
---|
| 77 | \snippet examples/widgets/lineedits/window.cpp 2
|
---|
| 78 |
|
---|
| 79 | QLineEdit supports the use of \l{QLineEdit::inputMask}{input masks}.
|
---|
| 80 | These only allow the user to type characters into the line edit that
|
---|
| 81 | follow a simple specification. We construct a group of widgets to
|
---|
| 82 | demonstrate a selection of predefined masks:
|
---|
| 83 |
|
---|
| 84 | \snippet examples/widgets/lineedits/window.cpp 3
|
---|
| 85 |
|
---|
| 86 | Another useful feature of QLineEdit is its ability to make its contents
|
---|
| 87 | read-only. This property is used to control access to a line edit in the
|
---|
| 88 | following group of widgets:
|
---|
| 89 |
|
---|
| 90 | \snippet examples/widgets/lineedits/window.cpp 4
|
---|
| 91 |
|
---|
| 92 | Now that all the child widgets have been constructed, we connect signals
|
---|
| 93 | from the comboboxes to slots in the \c Window object:
|
---|
| 94 |
|
---|
| 95 | \snippet examples/widgets/lineedits/window.cpp 5
|
---|
| 96 |
|
---|
| 97 | Each of these connections use the QComboBox::activated() signal that
|
---|
| 98 | supplies an integer to the slot. This will be used to efficiently
|
---|
| 99 | make changes to the appropriate line edit in each slot.
|
---|
| 100 |
|
---|
| 101 | We place each combobox, line edit, and label in a layout for each group
|
---|
| 102 | box, beginning with the layout for the \c echoGroup group box:
|
---|
| 103 |
|
---|
| 104 | \snippet examples/widgets/lineedits/window.cpp 6
|
---|
| 105 |
|
---|
| 106 | The other layouts are constructed in the same way:
|
---|
| 107 |
|
---|
| 108 | \snippet examples/widgets/lineedits/window.cpp 7
|
---|
| 109 |
|
---|
| 110 | Finally, we place each group box in a grid layout for the \c Window object
|
---|
| 111 | and set the window title:
|
---|
| 112 |
|
---|
| 113 | \snippet examples/widgets/lineedits/window.cpp 8
|
---|
| 114 |
|
---|
| 115 | The slots respond to signals emitted when the comboboxes are changed by the
|
---|
| 116 | user.
|
---|
| 117 |
|
---|
| 118 | When the combobox for the \gui{Echo} group box is changed, the \c echoChanged()
|
---|
| 119 | slot is called:
|
---|
| 120 |
|
---|
| 121 | \snippet examples/widgets/lineedits/window.cpp 9
|
---|
| 122 |
|
---|
| 123 | The slot updates the line edit in the same group box to use an echo mode that
|
---|
| 124 | corresponds to the entry described in the combobox.
|
---|
| 125 |
|
---|
| 126 | When the combobox for the \gui{Validator} group box is changed, the
|
---|
| 127 | \c validatorChanged() slot is called:
|
---|
| 128 |
|
---|
| 129 | \snippet examples/widgets/lineedits/window.cpp 10
|
---|
| 130 |
|
---|
| 131 | The slot either creates a new validator for the line edit to use, or it removes
|
---|
| 132 | the validator in use by calling QLineEdit::setValidator() with a zero pointer.
|
---|
| 133 | We clear the line edit in this case to ensure that the new validator is
|
---|
| 134 | initially given valid input to work with.
|
---|
| 135 |
|
---|
| 136 | When the combobox for the \gui{Alignment} group box is changed, the
|
---|
| 137 | \c alignmentChanged() slot is called:
|
---|
| 138 |
|
---|
| 139 | \snippet examples/widgets/lineedits/window.cpp 11
|
---|
| 140 |
|
---|
| 141 | This changes the way that text is displayed in the line edit to correspond with
|
---|
| 142 | the description selected in the combobox.
|
---|
| 143 |
|
---|
| 144 | The \c inputMaskChanged() slot handles changes to the combobox in the
|
---|
| 145 | \gui{Input Mask} group box:
|
---|
| 146 |
|
---|
| 147 | \snippet examples/widgets/lineedits/window.cpp 12
|
---|
| 148 |
|
---|
| 149 | Each entry in the relevant combobox is associated with an input mask. We set
|
---|
| 150 | a new mask by calling the QLineEdit::setMask() function with a suitable string;
|
---|
| 151 | the mask is disabled if an empty string is used.
|
---|
| 152 |
|
---|
| 153 | The \c accessChanged() slot handles changes to the combobox in the
|
---|
| 154 | \gui{Access} group box:
|
---|
| 155 |
|
---|
| 156 | \snippet examples/widgets/lineedits/window.cpp 13
|
---|
| 157 |
|
---|
| 158 | Here, we simply associate the \gui{False} and \gui{True} entries in the combobox
|
---|
| 159 | with \c false and \c true values to be passed to QLineEdit::setReadOnly(). This
|
---|
| 160 | allows the user to enable and disable input to the line edit.
|
---|
| 161 | */
|
---|