QIntValidator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
[[Category:snippets]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
You can prevent invalid text from being entered into a lineEdit using a validator. This example demonstrates QIntValidator, which only allows integers to be entered into the lineEdit.
You can prevent invalid text from being entered into a lineEdit using a validator. This example demonstrates QIntValidator, which only allows integers to be entered into the lineEdit.


==Validator.cpp==
== Validator.cpp ==
 
<code><br />#include &lt;QApplication&amp;gt;
 
#include &quot;form.h&amp;quot;
 
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);<br /> Form form;
 
form.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
 
== form.h ==
 
<code><br />#ifndef FORM_H<br />#define FORM_H
 
#include &quot;ui_form.h&amp;quot;
 
class Form : public QWidget, private Ui::Form<br />{<br /> Q_OBJECT
 
public:<br /> Form(QWidget *parent = 0);
 
public slots:
 
void on_lineEdit1_textEdited ( const QString &amp; text );<br /> void on_lineEdit2_textEdited ( const QString &amp; text );<br />};
 
#endif
 
</code>
 
== form.cpp ==
 
<code><br />#include &lt;QtGui&amp;gt;
 
#include &lt;iostream&amp;gt;
 
#include &quot;form.h&amp;quot;
 
Form::Form(QWidget '''parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);
<br /> // Limit input to valid values between 0 and 255. Either parent ('this' or 'lineEdit') works, I'm not sure what difference it makes.<br /> QIntValidator''' validator =<br /> //new QIntValidator(0, 255, this-&gt;lineEdit);<br /> new QIntValidator(0, 255, this);<br /> this-&gt;lineEdit1-&gt;setValidator(validator);<br /> this-&gt;lineEdit2-&gt;setValidator(validator);
 
}


==form.h==
void Form::on_lineEdit1_textEdited ( const QString &amp; text )<br />{<br /> std::cout &lt;&lt; &quot;text1: &quot; &lt;&lt; text.toStdString() &lt;&lt; std::endl;<br /> std::cout &lt;&lt; &quot;number1: &quot; &lt;&lt; text.toUInt() &lt;&lt; std::endl;<br />}


==form.cpp==
void Form::on_lineEdit2_textEdited ( const QString &amp; text )<br />{<br /> std::cout &lt;&lt; &quot;text2: &quot; &lt;&lt; text.toStdString() &lt;&lt; std::endl;<br />}


==form.ui==
</code>


===Categories:===
== form.ui ==


* [[:Category:snippets|snippets]]
<code><br />&amp;lt;?xml version=&quot;1.0&amp;quot; encoding=&quot;UTF-8&amp;quot;?&amp;gt;<br />&lt;ui version=&quot;4.0&amp;quot;&gt;<br /> &lt;class&amp;gt;Form&amp;lt;/class&amp;gt;<br /> &lt;widget class=&quot;QWidget&amp;quot; name=&quot;Form&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;0&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;0&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;400&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;300&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;property name=&quot;windowTitle&amp;quot;&gt;<br /> &lt;string&amp;gt;Form&amp;lt;/string&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;widget class=&quot;QLineEdit&amp;quot; name=&quot;lineEdit1&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;250&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;90&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;113&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;27&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;widget class=&quot;QLineEdit&amp;quot; name=&quot;lineEdit2&amp;quot;&gt;<br /> &lt;property name=&quot;geometry&amp;quot;&gt;<br /> &lt;rect&amp;gt;<br /> &lt;x&amp;gt;250&amp;lt;/x&amp;gt;<br /> &lt;y&amp;gt;170&amp;lt;/y&amp;gt;<br /> &lt;width&amp;gt;113&amp;lt;/width&amp;gt;<br /> &lt;height&amp;gt;27&amp;lt;/height&amp;gt;<br /> &lt;/rect&amp;gt;<br /> &lt;/property&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;/widget&amp;gt;<br /> &lt;resources/&amp;gt;<br /> &lt;connections/&amp;gt;<br />&lt;/ui&amp;gt;

Revision as of 11:21, 24 February 2015


[toc align_right="yes&quot; depth="3&quot;]

You can prevent invalid text from being entered into a lineEdit using a validator. This example demonstrates QIntValidator, which only allows integers to be entered into the lineEdit.

Validator.cpp

<br />#include &lt;QApplication&amp;gt;

#include &quot;form.h&amp;quot;

int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);<br /> Form form;

form.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br />

form.h

<br />#ifndef FORM_H<br />#define FORM_H

#include &quot;ui_form.h&amp;quot;

class Form : public QWidget, private Ui::Form<br />{<br /> Q_OBJECT

public:<br /> Form(QWidget *parent = 0);

public slots:

void on_lineEdit1_textEdited ( const QString &amp; text );<br /> void on_lineEdit2_textEdited ( const QString &amp; text );<br />};

#endif

form.cpp

<br />#include &lt;QtGui&amp;gt;

#include &lt;iostream&amp;gt;

#include &quot;form.h&amp;quot;

Form::Form(QWidget '''parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);
<br /> // Limit input to valid values between 0 and 255. Either parent ('this' or 'lineEdit') works, I'm not sure what difference it makes.<br /> QIntValidator''' validator =<br /> //new QIntValidator(0, 255, this-&gt;lineEdit);<br /> new QIntValidator(0, 255, this);<br /> this-&gt;lineEdit1-&gt;setValidator(validator);<br /> this-&gt;lineEdit2-&gt;setValidator(validator);

}

void Form::on_lineEdit1_textEdited ( const QString &amp; text )<br />{<br /> std::cout &lt;&lt; &quot;text1: &quot; &lt;&lt; text.toStdString() &lt;&lt; std::endl;<br /> std::cout &lt;&lt; &quot;number1: &quot; &lt;&lt; text.toUInt() &lt;&lt; std::endl;<br />}

void Form::on_lineEdit2_textEdited ( const QString &amp; text )<br />{<br /> std::cout &lt;&lt; &quot;text2: &quot; &lt;&lt; text.toStdString() &lt;&lt; std::endl;<br />}

form.ui


&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;
<ui version="4.0&quot;>
<class&gt;Form&lt;/class&gt;
<widget class="QWidget&quot; name="Form&quot;>
<property name="geometry&quot;>
<rect&gt;
<x&gt;0&lt;/x&gt;
<y&gt;0&lt;/y&gt;
<width&gt;400&lt;/width&gt;
<height&gt;300&lt;/height&gt;
</rect&gt;
</property&gt;
<property name="windowTitle&quot;>
<string&gt;Form&lt;/string&gt;
</property&gt;
<widget class="QLineEdit&quot; name="lineEdit1&quot;>
<property name="geometry&quot;>
<rect&gt;
<x&gt;250&lt;/x&gt;
<y&gt;90&lt;/y&gt;
<width&gt;113&lt;/width&gt;
<height&gt;27&lt;/height&gt;
</rect&gt;
</property&gt;
</widget&gt;
<widget class="QLineEdit&quot; name="lineEdit2&quot;>
<property name="geometry&quot;>
<rect&gt;
<x&gt;250&lt;/x&gt;
<y&gt;170&lt;/y&gt;
<width&gt;113&lt;/width&gt;
<height&gt;27&lt;/height&gt;
</rect&gt;
</property&gt;
</widget&gt;
</widget&gt;
<resources/&gt;
<connections/&gt;
</ui&gt;