×
Namespaces

Variants
Actions

How to use QIntValidator in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): Qt
Symbian
Article
Keywords: Qvalidator,QLineEdit,QIntValidator
Created: mind_freak (20 Apr 2009)
Last edited: hamishwillee (11 Oct 2012)

Contents

Introduction

The QIntValidator class provides a validation that ensures a string contains a valid integer within a specified range.

Values consisting of a number of digits equal to or less than the max value are considered intermediate.

The minimum and maximum values are set in one call with setRange(), or individually with setBottom() and setTop().

Various Functions

Note: val is object of QValidator.

  • This property holds the validator's lowest acceptable value.
val->setbottom(1);
  • This property holds the validator's highest acceptable value.
val->setTop(10);


Source File

main.cpp

#include <QtGui/QApplication>
#include "valid.h"
#include<QWidget>
#include<QLineEdit>
#include<QVBoxLayout>
#include <QIntValidator>
#include<QValidator>
#include<QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win=new QWidget();
QLabel *lbl=new QLabel("QIntValidator");
QLineEdit *edit = new QLineEdit();
lbl->setBuddy(edit);
QValidator *val=new QIntValidator(1,10,edit);
edit->setValidator(val);
QVBoxLayout *lay=new QVBoxLayout();
lay->addWidget(lbl);
lay->addWidget(edit);
win->setLayout(lay);
win->show();
return a.exec();
}

Note: From the above code validator we can write the integer value between 1 to 10.You can't write value more than 10.

Screenshot

More AboutQIntValidator..

Intvalid.JPG As you can see in the above image that we are unable to enter three digit value in QLineEdit,because the last and maximum possible value is "10" that is 2 digit.


Intvalid1.JPG From above image we can say that after pressing the "1" in keypad the Validator does not allowing us enter "11" in line edit as it maximum value is "10"

More AboutQIntValidator..

This page was last modified on 11 October 2012, at 04:17.
122 page views in the last 30 days.
×