QIntValidator: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
[[Category:snippets]]<br />[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. | 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 <QApplication&gt; | |||
#include "form.h&quot; | |||
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);<br /> Form form; | |||
form.show();<br /> return app.exec&amp;#40;&#41;;<br />}<br /></code> | |||
== form.h == | |||
<code><br />#ifndef FORM_H<br />#define FORM_H | |||
#include "ui_form.h&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 & text );<br /> void on_lineEdit2_textEdited ( const QString & text );<br />}; | |||
#endif | |||
</code> | |||
== form.cpp == | |||
<code><br />#include <QtGui&gt; | |||
#include <iostream&gt; | |||
#include "form.h&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->lineEdit);<br /> new QIntValidator(0, 255, this);<br /> this->lineEdit1->setValidator(validator);<br /> this->lineEdit2->setValidator(validator); | |||
} | |||
void Form::on_lineEdit1_textEdited ( const QString & text )<br />{<br /> std::cout << "text1: " << text.toStdString() << std::endl;<br /> std::cout << "number1: " << text.toUInt() << std::endl;<br />} | |||
void Form::on_lineEdit2_textEdited ( const QString & text )<br />{<br /> std::cout << "text2: " << text.toStdString() << std::endl;<br />} | |||
</code> | |||
== | == form.ui == | ||
<code><br />&lt;?xml version="1.0&quot; encoding="UTF-8&quot;?&gt;<br /><ui version="4.0&quot;><br /> <class&gt;Form&lt;/class&gt;<br /> <widget class="QWidget&quot; name="Form&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;0&lt;/x&gt;<br /> <y&gt;0&lt;/y&gt;<br /> <width&gt;400&lt;/width&gt;<br /> <height&gt;300&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> <property name="windowTitle&quot;><br /> <string&gt;Form&lt;/string&gt;<br /> </property&gt;<br /> <widget class="QLineEdit&quot; name="lineEdit1&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;250&lt;/x&gt;<br /> <y&gt;90&lt;/y&gt;<br /> <width&gt;113&lt;/width&gt;<br /> <height&gt;27&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> </widget&gt;<br /> <widget class="QLineEdit&quot; name="lineEdit2&quot;><br /> <property name="geometry&quot;><br /> <rect&gt;<br /> <x&gt;250&lt;/x&gt;<br /> <y&gt;170&lt;/y&gt;<br /> <width&gt;113&lt;/width&gt;<br /> <height&gt;27&lt;/height&gt;<br /> </rect&gt;<br /> </property&gt;<br /> </widget&gt;<br /> </widget&gt;<br /> <resources/&gt;<br /> <connections/&gt;<br /></ui&gt; |
Revision as of 11:21, 24 February 2015
[toc align_right="yes" depth="3"]
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 <QApplication&gt;
#include "form.h&quot;
int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);<br /> Form form;
form.show();<br /> return app.exec&amp;#40;&#41;;<br />}<br />
form.h
<br />#ifndef FORM_H<br />#define FORM_H
#include "ui_form.h&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 & text );<br /> void on_lineEdit2_textEdited ( const QString & text );<br />};
#endif
form.cpp
<br />#include <QtGui&gt;
#include <iostream&gt;
#include "form.h&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->lineEdit);<br /> new QIntValidator(0, 255, this);<br /> this->lineEdit1->setValidator(validator);<br /> this->lineEdit2->setValidator(validator);
}
void Form::on_lineEdit1_textEdited ( const QString & text )<br />{<br /> std::cout << "text1: " << text.toStdString() << std::endl;<br /> std::cout << "number1: " << text.toUInt() << std::endl;<br />}
void Form::on_lineEdit2_textEdited ( const QString & text )<br />{<br /> std::cout << "text2: " << text.toStdString() << std::endl;<br />}
form.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QLineEdit" name="lineEdit1">
<property name="geometry">
<rect>
<x>250</x>
<y>90</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit2">
<property name="geometry">
<rect>
<x>250</x>
<y>170</y>
<width>113</width>
<height>27</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>