Validators in QT

Validators are the input restriction filters we can apply up on our input fields like QLineEdit. We can make validators and apply to them so that they can be made to accept only numbers, capital letters, etc..

QT have some predefined validators. Eg for int,Regular expression etc.

We can create our own validators by subclassing QValidator class.

Example: Validator for Digits 0-9

#include “QValidator.h”

//Derive from Qvalidator
class DigitValidator:public QValidator

    //Validation function. Pure virtual in QValidator
    inline State validate( QString & input, int & pos ) const

        //Create a regular expression for the condition
        QRegExp vRX;

       //Search for it in the input
            return QValidator::Invalid;
            return QValidator::Acceptable;

void main()
    QLineEdit *txtInPut;
    txtInPut->setValidator(new DigitValidator);

Now the txtInPut line edit  box will only accept the input if its 0-9 other characters cant be entered.

how to use QT QProgressBar to show busy/indefinite status

A progress bar is used to give the user an indication of the progress of an operation and to reassure them that the application is still running.

The progress bar uses the concept of steps. You set it up by specifying the minimum and maximum possible step values, and it will display the percentage of steps that have been completed when you later give it the current step value.

In QT we can use QProgressBar widget which provides a horizontal or vertical progress bar for the same.

To show busy or indefinite time in progress bar using QT, just set the maximum and minimum limits of QProgressBar to 0.

QProgressBar *pbarDL;
//To start indefinite/busy progress bar
//To stop just set any value to maximum
And you will get
QProgressBar showing Busy status

QProgressBar showing Busy status

That’s it.. Easy enough na..Winking smile

How to use QTabWidget

Declaration and Adding Tabs

QTabWidget MainIFaceTab = new QTabWidget(this);

MainIFaceTab->addTab(new QWidget(),”Tab1”);

MainIFaceTab->addTab(new QWidget(),”Tab2”);”

Getting widget back from a tab

QWidget *wgt = MainIFaceTab->widget(0); //Returns widget of Tab1(Index starts from 0)

Setting a widget to Tab

We need to

1. Create a layout.

2. Add the widget to be set to the layout

3. Set the layout to the returned widget from Tab.

QWidget *wgt = MainIFaceTab->widget(0); //Returns widget of Tab1(Index starts from 0)

QVBoxLayout *vBoxLay = new QVBoxLayout; //create layout

vBoxLay->addWidget(qTable); //Adds a table widget to the layout

wgt->setLayout(vBoxLay); //Set the layout with the table to the returned widget.