qvalidator.html 16.2 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qvalidator.cpp -->
  <title>QValidator Class | Qt GUI 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qtgui-index.html">Qt GUI</a></td><td ><a href="qtgui-module.html">C++ Classes</a></td><td >QValidator</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-types">Public Types</a></li>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#signals">Signals</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QValidator Class</h1>
<!-- $$$QValidator-brief -->
<p>The <a href="qvalidator.html">QValidator</a> class provides validation of input text. <a href="#details">More...</a></p>
<!-- @@@QValidator -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QValidator&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += gui</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> <a href="../qtcore/qobject.html">QObject</a></td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherited By:</td><td class="memItemRight bottomAlign"> <p><a href="qdoublevalidator.html">QDoubleValidator</a>, <a href="qintvalidator.html">QIntValidator</a>, <a href="qregexpvalidator.html">QRegExpValidator</a>, and <a href="qregularexpressionvalidator.html">QRegularExpressionValidator</a></p>
</td></tr></table></div><ul>
<li><a href="qvalidator-members.html">List of all members, including inherited members</a></li>
</ul>
<a name="public-types"></a>
<h2 id="public-types">Public Types</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> enum </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#State-enum">State</a></b> { Invalid, Intermediate, Acceptable }</td></tr>
</table></div>
<a name="public-functions"></a>
<h2 id="public-functions">Public Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#QValidator">QValidator</a></b>(QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#dtor.QValidator">~QValidator</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#fixup">fixup</a></b>(QString &amp;<i>input</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QLocale </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#locale">locale</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#setLocale">setLocale</a></b>(const QLocale &amp;<i>locale</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QValidator::State </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#validate">validate</a></b>(QString &amp;<i>input</i>, int &amp;<i>pos</i>) const = 0</td></tr>
</table></div>
<ul>
<li class="fn">34 public functions inherited from <a href="../qtcore/qobject.html#public-functions">QObject</a></li>
</ul>
<a name="signals"></a>
<h2 id="signals">Signals</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qvalidator.html#changed">changed</a></b>()</td></tr>
</table></div>
<ul>
<li class="fn">2 signals inherited from <a href="../qtcore/qobject.html#signals">QObject</a></li>
</ul>
<h3>Additional Inherited Members</h3>
<ul>
<li class="fn">1 property inherited from <a href="../qtcore/qobject.html#properties">QObject</a></li>
<li class="fn">1 public slot inherited from <a href="../qtcore/qobject.html#public-slots">QObject</a></li>
<li class="fn">1 public variable inherited from <a href="../qtcore/qobject.html#public-variables">QObject</a></li>
<li class="fn">10 static public members inherited from <a href="../qtcore/qobject.html#static-public-members">QObject</a></li>
<li class="fn">9 protected functions inherited from <a href="../qtcore/qobject.html#protected-functions">QObject</a></li>
<li class="fn">2 protected variables inherited from <a href="../qtcore/qobject.html#protected-variables">QObject</a></li>
</ul>
<a name="details"></a>
<!-- $$$QValidator-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The <a href="qvalidator.html">QValidator</a> class provides validation of input text.</p>
<p>The class itself is abstract. Two subclasses, <a href="qintvalidator.html">QIntValidator</a> and <a href="qdoublevalidator.html">QDoubleValidator</a>, provide basic numeric-range checking, and <a href="qregexpvalidator.html">QRegExpValidator</a> provides general checking using a custom regular expression.</p>
<p>If the built-in validators aren't sufficient, you can subclass <a href="qvalidator.html">QValidator</a>. The class has two virtual functions: <a href="qvalidator.html#validate">validate</a>() and <a href="qvalidator.html#fixup">fixup</a>().</p>
<p><a href="qvalidator.html#validate">validate</a>() must be implemented by every subclass. It returns <a href="qvalidator.html#State-enum">Invalid</a>, <a href="qvalidator.html#State-enum">Intermediate</a> or <a href="qvalidator.html#State-enum">Acceptable</a> depending on whether its argument is valid (for the subclass's definition of valid).</p>
<p>These three states require some explanation. An <a href="qvalidator.html#State-enum">Invalid</a> string is <i>clearly</i> invalid. <a href="qvalidator.html#State-enum">Intermediate</a> is less obvious: the concept of validity is difficult to apply when the string is incomplete (still being edited). <a href="qvalidator.html">QValidator</a> defines <a href="qvalidator.html#State-enum">Intermediate</a> as the property of a string that is neither clearly invalid nor acceptable as a final result. <a href="qvalidator.html#State-enum">Acceptable</a> means that the string is acceptable as a final result. One might say that any string that is a plausible intermediate state during entry of an <a href="qvalidator.html#State-enum">Acceptable</a> string is <a href="qvalidator.html#State-enum">Intermediate</a>.</p>
<p>Here are some examples:</p>
<ul>
<li>For a line edit that accepts integers from 10 to 1000 inclusive, 42 and 123 are <a href="qvalidator.html#State-enum">Acceptable</a>, the empty string and 5 are <a href="qvalidator.html#State-enum">Intermediate</a>, and &quot;asdf&quot; and 1114 is <a href="qvalidator.html#State-enum">Invalid</a>.</li>
<li>For an editable combobox that accepts URLs, any well-formed URL is <a href="qvalidator.html#State-enum">Acceptable</a>, &quot;http://example.com/,&quot; is <a href="qvalidator.html#State-enum">Intermediate</a> (it might be a cut and paste action that accidentally took in a comma at the end), the empty string is <a href="qvalidator.html#State-enum">Intermediate</a> (the user might select and delete all of the text in preparation for entering a new URL) and &quot;http:///./&quot; is <a href="qvalidator.html#State-enum">Invalid</a>.</li>
<li>For a spin box that accepts lengths, &quot;11cm&quot; and &quot;1in&quot; are <a href="qvalidator.html#State-enum">Acceptable</a>, &quot;11&quot; and the empty string are <a href="qvalidator.html#State-enum">Intermediate</a>, and &quot;http://example.com&quot; and &quot;hour&quot; are <a href="qvalidator.html#State-enum">Invalid</a>.</li>
</ul>
<p><a href="qvalidator.html#fixup">fixup</a>() is provided for validators that can repair some user errors. The default implementation does nothing. <a href="../qtwidgets/qlineedit.html">QLineEdit</a>, for example, will call <a href="qvalidator.html#fixup">fixup</a>() if the user presses Enter (or Return) and the content is not currently valid. This allows the <a href="qvalidator.html#fixup">fixup</a>() function the opportunity of performing some magic to make an <a href="qvalidator.html#State-enum">Invalid</a> string <a href="qvalidator.html#State-enum">Acceptable</a>.</p>
<p>A validator has a locale, set with <a href="qvalidator.html#setLocale">setLocale</a>(). It is typically used to parse localized data. For example, <a href="qintvalidator.html">QIntValidator</a> and <a href="qdoublevalidator.html">QDoubleValidator</a> use it to parse localized representations of integers and doubles.</p>
<p><a href="qvalidator.html">QValidator</a> is typically used with <a href="../qtwidgets/qlineedit.html">QLineEdit</a>, <a href="../qtwidgets/qspinbox.html">QSpinBox</a> and <a href="../qtwidgets/qcombobox.html">QComboBox</a>.</p>
</div>
<p><b>See also </b><a href="qintvalidator.html">QIntValidator</a>, <a href="qdoublevalidator.html">QDoubleValidator</a>, <a href="qregexpvalidator.html">QRegExpValidator</a>, and <a href="../qtwidgets/qtwidgets-widgets-lineedits-example.html">Line Edits Example</a>.</p>
<!-- @@@QValidator -->
<div class="types">
<h2>Member Type Documentation</h2>
<!-- $$$State$$$Invalid$$$Intermediate$$$Acceptable -->
<h3 class="fn" id="State-enum"><a name="State-enum"></a>enum QValidator::<span class="name">State</span></h3>
<p>This enum type defines the states in which a validated string can exist.</p>
<div class="table"><table class="valuelist"><tr valign="top" class="odd"><th class="tblConst">Constant</th><th class="tblval">Value</th><th class="tbldscr">Description</th></tr>
<tr><td class="topAlign"><code>QValidator::Invalid</code></td><td class="topAlign tblval"><code>0</code></td><td class="topAlign">The string is <i>clearly</i> invalid.</td></tr>
<tr><td class="topAlign"><code>QValidator::Intermediate</code></td><td class="topAlign tblval"><code>1</code></td><td class="topAlign">The string is a plausible intermediate value.</td></tr>
<tr><td class="topAlign"><code>QValidator::Acceptable</code></td><td class="topAlign tblval"><code>2</code></td><td class="topAlign">The string is acceptable as a final result; i.e&#x2e; it is valid.</td></tr>
</table></div>
<!-- @@@State -->
</div>
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QValidator[overload1]$$$QValidatorQObject* -->
<h3 class="fn" id="QValidator"><a name="QValidator"></a>QValidator::<span class="name">QValidator</span>(<span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>Sets up the validator. The <i>parent</i> parameter is passed on to the <a href="../qtcore/qobject.html">QObject</a> constructor.</p>
<!-- @@@QValidator -->
<!-- $$$~QValidator[overload1]$$$~QValidator -->
<h3 class="fn" id="dtor.QValidator"><a name="dtor.QValidator"></a><code>[virtual] </code>QValidator::<span class="name">~QValidator</span>()</h3>
<p>Destroys the validator, freeing any storage and other resources used.</p>
<!-- @@@~QValidator -->
<!-- $$$changed[overload1]$$$changed -->
<h3 class="fn" id="changed"><a name="changed"></a><code>[signal] </code><span class="type">void</span> QValidator::<span class="name">changed</span>()</h3>
<p>This signal is emitted when any property that may affect the validity of a string has changed.</p>
<!-- @@@changed -->
<!-- $$$fixup[overload1]$$$fixupQString& -->
<h3 class="fn" id="fixup"><a name="fixup"></a><code>[virtual] </code><span class="type">void</span> QValidator::<span class="name">fixup</span>(<span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>input</i>) const</h3>
<p>This function attempts to change <i>input</i> to be valid according to this validator's rules. It need not result in a valid string: callers of this function must re-test afterwards; the default does nothing.</p>
<p>Reimplementations of this function can change <i>input</i> even if they do not produce a valid string. For example, an ISBN validator might want to delete every character except digits and &quot;-&quot;, even if the result is still not a valid ISBN; a surname validator might want to remove whitespace from the start and end of the string, even if the resulting string is not in the list of accepted surnames.</p>
<!-- @@@fixup -->
<!-- $$$locale[overload1]$$$locale -->
<h3 class="fn" id="locale"><a name="locale"></a><span class="type"><a href="../qtcore/qlocale.html">QLocale</a></span> QValidator::<span class="name">locale</span>() const</h3>
<p>Returns the locale for the validator. The locale is by default initialized to the same as QLocale().</p>
<p><b>See also </b><a href="qvalidator.html#setLocale">setLocale</a>() and <a href="../qtcore/qlocale.html#QLocale">QLocale::QLocale</a>().</p>
<!-- @@@locale -->
<!-- $$$setLocale[overload1]$$$setLocaleconstQLocale& -->
<h3 class="fn" id="setLocale"><a name="setLocale"></a><span class="type">void</span> QValidator::<span class="name">setLocale</span>(const <span class="type"><a href="../qtcore/qlocale.html">QLocale</a></span> &amp;<i>locale</i>)</h3>
<p>Sets the <i>locale</i> that will be used for the validator. Unless setLocale has been called, the validator will use the default locale set with <a href="../qtcore/qlocale.html#setDefault">QLocale::setDefault</a>(). If a default locale has not been set, it is the operating system's locale.</p>
<p><b>See also </b><a href="qvalidator.html#locale">locale</a>() and <a href="../qtcore/qlocale.html#setDefault">QLocale::setDefault</a>().</p>
<!-- @@@setLocale -->
<!-- $$$validate[overload1]$$$validateQString&int& -->
<h3 class="fn" id="validate"><a name="validate"></a><code>[pure virtual] </code><span class="type"><a href="qvalidator.html#State-enum">QValidator::State</a></span> QValidator::<span class="name">validate</span>(<span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>input</i>, <span class="type">int</span> &amp;<i>pos</i>) const</h3>
<p>This virtual function returns <a href="qvalidator.html#State-enum">Invalid</a> if <i>input</i> is invalid according to this validator's rules, <a href="qvalidator.html#State-enum">Intermediate</a> if it is likely that a little more editing will make the input acceptable (e.g&#x2e; the user types &quot;4&quot; into a widget which accepts integers between 10 and 99), and <a href="qvalidator.html#State-enum">Acceptable</a> if the input is valid.</p>
<p>The function can change both <i>input</i> and <i>pos</i> (the cursor position) if required.</p>
<!-- @@@validate -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>