-2

I need to control the input that is being provided through a QLineEdit. The provided input will be checked to see if it meets certain criteria (i.e. particular letters, digits and symbols constraints)

Tasks:

  1. This checking procedure has to be done when the user is providing input.
  2. Due to the mismatch with the constraint list, a message should pop-up to notify the user.

My Problem: I only can see how to check the text after the user presses any pushbutton while my goal is to check the input while it is being provided and not have to wait until the button is pressed.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QString>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QString reg_exp("!§$%&/(){}[]=?\ß`*+~#'.,;-@");
    int flag = 0;

    QString str = ui->lineEdit->text();


    for(int i = 0; i < str.length(); ++i)
    {
         for(int j = 0; j < reg_exp.length(); ++j )
         {
              if(str[i] == reg_exp[j])
              flag = 1;
         }
         if (flag == 1)
         {
            QMessageBox::warning(this,"Check","Its a mismatch!");
             break;
         }
    }
}
nfranklin
  • 385
  • 1
  • 8
eklavay.tonoy
  • 23
  • 1
  • 6

2 Answers2

0

You can use the QLineEdit::textEdited signal: create a slot for it, and then put the contents of your function MainWindow::on_pushButton_clicked in the new slot function (by default this would be MainWindow::on_lineEdit_textEdited(const QString &arg1)).

agold
  • 6,140
  • 9
  • 38
  • 54
0

Here is the finised code, but in a very simple version. I have not used any regular expression validation.

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

    private slots:
        void on_lineEdit_textEdited(const QString &str);

    private:
        Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->lineEdit,static_cast<void (QLineEdit::*)(const QString&)>(&QLineEdit::textEdited),this,&MainWindow::on_lineEdit_textEdited);
    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }

    void MainWindow::on_lineEdit_textEdited(const QString &str)
{
    int flag = 0;

    for(int i = 0; i < str.length(); ++i)
    {
        if((str[i] >= 'A') && (str[i] <= 'Z') || (str[i] >= 'a') && (str[i] <= 'z') || (str[i] == '_') || (str[i] >= '0') && (str[i] <= '9') )
          {
            ui->lineEdit->setStyleSheet("QLineEdit{border: 2px solid green}");

          }
        else
        {
            ui->lineEdit->setStyleSheet("QLineEdit{border: 2px solid red}");
            flag = 1;

            break;
        }
    }

    if(flag == 1)
        QMessageBox::warning(this,"Check","Wrong Input!");
}
eklavay.tonoy
  • 23
  • 1
  • 6