Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

QIntValidator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Cleanup)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:snippets]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]
{{LangSwitch}}
 
[[Category:snippets]]
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 {{DocLink|QIntValidator}}, which only allows integers to be entered into the lineEdit.


== Validator.cpp ==
== Validator.cpp ==


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


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


int main(int argc, char *argv[])<br />{<br /> QApplication app(argc, argv);<br /> Form form;
int main(int argc, char *argv[])
 
{
form.show();<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code>
    QApplication app(argc, argv);
    Form form;
    form.show();
    return app.exec();
}
</code>


== form.h ==
== form.h ==


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


#include &quot;ui_form.h&amp;quot;
#include "ui_form.h"
 
class Form : public QWidget, private Ui::Form<br />{<br /> Q_OBJECT
 
public:<br /> Form(QWidget *parent = 0);


class Form : public QWidget, private Ui::Form
{
    Q_OBJECT
public:
    Form(QWidget* parent=0);
public slots:
public slots:
 
    void on_lineEdit1_textEdited( const QString & text );
void on_lineEdit1_textEdited ( const QString &amp; text );<br /> void on_lineEdit2_textEdited ( const QString &amp; text );<br />};
    void on_lineEdit2_textEdited( const QString & text );
};


#endif
#endif
</code>
</code>


== form.cpp ==
== form.cpp ==


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


#include &lt;iostream&amp;gt;
#include "form.h"


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


Form::Form(QWidget '''parent)<br /> : QWidget(parent)<br />{<br /> setupUi(this);
void Form::on_lineEdit1_textEdited(const QString& text)
<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);
{
    std::cout << "text1: " << text.toStdString() << std::endl;
    std::cout << "number1: " << text.toUInt() << std::endl;
}


void Form::on_lineEdit2_textEdited(const QString& text)
{
    std::cout << "text2: " << text.toStdString() << std::endl;
}
}
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 />}


</code>
</code>
Line 52: Line 75:
== form.ui ==
== form.ui ==


<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;
<code>
<?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>
</code>

Latest revision as of 22:03, 28 June 2015

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

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

#include <QApplication>

#include "form.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Form form;
    form.show();
    return app.exec();
}

form.h

#ifndef FORM_H
#define FORM_H

#include "ui_form.h"

class Form : public QWidget, private Ui::Form
{
    Q_OBJECT
public:
    Form(QWidget* parent=0);
public slots:
    void on_lineEdit1_textEdited( const QString & text );
    void on_lineEdit2_textEdited( const QString & text );
};

#endif

form.cpp

#include <QtGui>
#include <iostream>

#include "form.h"

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

void Form::on_lineEdit1_textEdited(const QString& text)
{
    std::cout << "text1: " << text.toStdString() << std::endl;
    std::cout << "number1: " << text.toUInt() << std::endl;
}

void Form::on_lineEdit2_textEdited(const QString& text)
{
    std::cout << "text2: " << text.toStdString() << std::endl;
}

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>