QIntValidator: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Decode HTML entity names)
(Cleanup)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:snippets]]
[[Category:snippets]]
[toc align_right="yes" depth="3"]
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.
 
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 ==
Line 15: Line 12:
int main(int argc, char *argv[])
int main(int argc, char *argv[])
{
{
QApplication app(argc, argv);
    QApplication app(argc, argv);
Form form;
    Form form;
 
    form.show();
form.show();
    return app.exec();
return app.exec();
}
}
</code>
</code>
Line 33: Line 29:
class Form : public QWidget, private Ui::Form
class Form : public QWidget, private Ui::Form
{
{
Q_OBJECT
    Q_OBJECT
 
public:
public:
Form(QWidget *parent = 0);
    Form(QWidget* parent=0);
 
public slots:
public slots:
 
    void on_lineEdit1_textEdited( const QString & text );
void on_lineEdit1_textEdited ( const QString & text );
    void on_lineEdit2_textEdited( const QString & text );
void on_lineEdit2_textEdited ( const QString & text );
};
};


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


Line 52: Line 44:
<code>
<code>
#include <QtGui>
#include <QtGui>
#include <iostream>
#include <iostream>


#include "form.h"
#include "form.h"


Form::Form(QWidget '''parent)
Form::Form(QWidget *parent)
: QWidget(parent)
    : QWidget(parent)
{
{
setupUi(this);
    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.
// 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 =
QIntValidator''' validator =
    //new QIntValidator(0, 255, this->lineEdit);
//new QIntValidator(0, 255, this->lineEdit);
        new QIntValidator(0, 255, this);
new QIntValidator(0, 255, this);
    this->lineEdit1->setValidator(validator);
this->lineEdit1->setValidator(validator);
    this->lineEdit2->setValidator(validator);
this->lineEdit2->setValidator(validator);
 
}
}


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


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


Line 126: Line 115:
  <connections/>
  <connections/>
</ui>
</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>