Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get characters which user is typing in QLineEdit ?
Forum Updated to NodeBB v4.3 + New Features

How to get characters which user is typing in QLineEdit ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlineeditqt5.7
16 Posts 6 Posters 8.2k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by
    #1

    Hi
    I am creating a forum in my app that has a field called 'Name' and i wanna check if user is typing an alphabet or a number or a specials symbol or all of them how can i do that ? remember i should do all this while user is typing his/her name not after he clicks on the submit button.

    what is a signature ?? Lol

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

      May InputMask help?

      Edit: not the way to go, I'd rather use some text validator, see setValidator.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      4
      • M Offline
        M Offline
        MaxDevI
        wrote on last edited by
        #3

        Well, if you have a lineEdit where the user is typing into, you can simply connect the lineEdit using the signal textChanged. Let's say you have a lineEdit inside a mainWindow class, you could do something like this.

        QLineEdit *myLineEdit = new QLineEdit(this);
         
        connect (myLineEdit, SIGNAL(textChanged(const QString & text)), this, SLOT(checkChange(const QString & text)));
        

        where "this" is the mainWindow instance. Now all you need to do is implement a slot in your mainWindow class, maybe something like this?

        void MainWindow::checkChange(const QString & text){
            //do what you want to do here
            qDebug() << text;
        }
        

        Now whenever the user types anything into the lineEdit, or changes it using QLineEdit->setText(), you will have what they are typing in the text parameter in checkChange. In my checkChange, you can see that I am printing the text to the Debug. Hope this helps.

        1 Reply Last reply
        1
        • Pradeep KumarP Offline
          Pradeep KumarP Offline
          Pradeep Kumar
          wrote on last edited by
          #4

          HI,

          Adding to @MaxDevI you can use QLabel also,along with debug statements, u will get the value appended to your QLabel.

          Here is the sample code.

          connect(m_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);
          

          }

          use connect statement as provided above,

          and to check the values you are entering will gets reflected in QLabel, so u can verify which characters are u typing, whether it is a numbers or letters.

          void LineEditProgram::getLineEditValue(QString stringValue)
          {
          qDebug () << "value changed" << stringValue;
          m_label->setText(stringValue);
          }

          Pradeep Kumar
          Qt,QML Developer

          AhtiA 1 Reply Last reply
          2
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            If you want to restrict digits, letters, symbols. u can also use QRegularExpression. So u can set it to QLineEdit using setValidator.

            As the link provided by @JohanSolo , so can go through the link also.

            Sample Code.

            QRegularExpression regex("^[a-zA-Z0-9]*$");
            QValidator *validator = new QRegularExpressionValidator(regex, this);
            m_lineEdit->setValidator(validator);
            

            Which accepts letters and digits not symbols

            Pradeep Kumar
            Qt,QML Developer

            1 Reply Last reply
            3
            • Pradeep KumarP Pradeep Kumar

              HI,

              Adding to @MaxDevI you can use QLabel also,along with debug statements, u will get the value appended to your QLabel.

              Here is the sample code.

              connect(m_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);
              

              }

              use connect statement as provided above,

              and to check the values you are entering will gets reflected in QLabel, so u can verify which characters are u typing, whether it is a numbers or letters.

              void LineEditProgram::getLineEditValue(QString stringValue)
              {
              qDebug () << "value changed" << stringValue;
              m_label->setText(stringValue);
              }

              AhtiA Offline
              AhtiA Offline
              Ahti
              wrote on last edited by
              #6

              @Pradeep-Kumar @MaxDevI @JohanSolo

              I just wanna change the border color of LineEdit to red whenever user types any special symbol except a space and wanna turn the color back to green if he types a number or alphabet.

              what is a signature ?? Lol

              1 Reply Last reply
              0
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by
                #7
                1. So if you use as mentioned earlier in same post , you can use the below code to restrict special characters.

                QRegularExpression regex("^[a-zA-Z0-9_]*$");
                QValidator *validator = new QRegularExpressionValidator(regex, this);
                m_lineEdit->setValidator(validator);

                1. If you want to change the colour with respect to characters entered you can go for the below code.
                  Then dont use validator
                  connect(m_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);

                use connection statement to get values when you type in QLineEdit.

                And Below Slot can be used to observe the changes done while typing in QLineEdit, with respect to characters, when u enter special characters Color changes to Red, and Color changes to Green when entering the digits or letters.

                void LineEditProgram::getLineEditValue(QString stringValue)
                {
                qDebug () << "value changed" << stringValue;

                QString temp = m_lineEdit->text();
                
                QRegExp regexp("^[0-9A-Za-z_]*$");
                
                if (!regexp.exactMatch(temp))
                {
                    qDebug () << "colour changed to red" <<  stringValue;
                    m_lineEdit->setStyleSheet("border: 5px solid red");
                    m_label->setText(stringValue);
                }
                else
                {
                    qDebug () << "colour changed to green" <<  stringValue;
                    m_lineEdit->setStyleSheet("border: 5px solid green");
                    m_label->setText(stringValue);
                }
                

                }

                Pradeep Kumar
                Qt,QML Developer

                N 1 Reply Last reply
                4
                • Pradeep KumarP Pradeep Kumar
                  1. So if you use as mentioned earlier in same post , you can use the below code to restrict special characters.

                  QRegularExpression regex("^[a-zA-Z0-9_]*$");
                  QValidator *validator = new QRegularExpressionValidator(regex, this);
                  m_lineEdit->setValidator(validator);

                  1. If you want to change the colour with respect to characters entered you can go for the below code.
                    Then dont use validator
                    connect(m_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);

                  use connection statement to get values when you type in QLineEdit.

                  And Below Slot can be used to observe the changes done while typing in QLineEdit, with respect to characters, when u enter special characters Color changes to Red, and Color changes to Green when entering the digits or letters.

                  void LineEditProgram::getLineEditValue(QString stringValue)
                  {
                  qDebug () << "value changed" << stringValue;

                  QString temp = m_lineEdit->text();
                  
                  QRegExp regexp("^[0-9A-Za-z_]*$");
                  
                  if (!regexp.exactMatch(temp))
                  {
                      qDebug () << "colour changed to red" <<  stringValue;
                      m_lineEdit->setStyleSheet("border: 5px solid red");
                      m_label->setText(stringValue);
                  }
                  else
                  {
                      qDebug () << "colour changed to green" <<  stringValue;
                      m_lineEdit->setStyleSheet("border: 5px solid green");
                      m_label->setText(stringValue);
                  }
                  

                  }

                  N Offline
                  N Offline
                  nanor
                  wrote on last edited by
                  #8

                  @Pradeep-Kumar Hi. I am working on a project that I have to make a lineEdit which the user only allows to type numbers in it(not characters) . Also, I want to change the lineEdit's border color (If the user types a character, the color of the lineEdit's border is red and if the user types number, the color must be green). With the help of your code, I wrote the following code, but I got the error "F:\lineeditvalidator\mainwindow.cpp:42: error: expected type-specifier before 'QRegularExpressionValidator'
                  QValidator *validator = new QRegularExpressionValidator(regexp, this);
                  ^"
                  I will appreciate your help on this matter.

                  mainwindow.h:

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  #include <QRegularExpression>
                  #include <QValidator>
                  #include <QRegularExpressionMatch>
                  #include <QDebug>
                  #include <QRegularExpressionValidator>
                  namespace Ui {
                  class MainWindow;
                  }
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      explicit MainWindow(QWidget *parent = 0);
                      ~MainWindow();
                  
                      void QRegularExpressionValidator();
                  
                  
                  private:
                      Ui::MainWindow *ui;
                  
                  public slots:
                      void getLineEditValue(QString stringValue);
                  
                  };
                  
                  #endif // MAINWINDOW_H
                  

                  main.cpp:

                  #include "mainwindow.h"
                  #include <QApplication>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                      MainWindow w;
                      w.show();
                  
                      return a.exec();
                  }
                  

                  mainwindow.cpp:

                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  #include <QRegularExpression>
                  #include <QValidator>
                  #include <QRegularExpressionMatch>
                  #include <QDebug>
                  #include <QRegularExpressionValidator>
                  
                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                  
                      
                      connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);
                  
                  
                   }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  
                  void MainWindow::getLineEditValue(QString stringValue)
                  {
                  
                  
                      qDebug () << "1" << stringValue;
                  
                      QString temp = ui->lineEdit->text();
                  
                      QRegExp regexp("^[0-9A]*$");
                  
                      QValidator *validator = new QRegularExpressionValidator(regexp, this);
                      ui->lineEdit->setValidator(validator);
                  
                      if (!regexp.exactMatch(temp))
                       {
                          qDebug () << "colour changed to red" <<  stringValue;
                           ui->lineEdit->setStyleSheet("border: 5px solid red");}
                  
                      else
                      {
                          qDebug () << "colour changed to green" <<  stringValue;
                          ui->lineEdit->setStyleSheet("border: 5px solid green");
                  
                      }
                  
                  }
                  
                  
                  JonBJ 1 Reply Last reply
                  0
                  • N nanor

                    @Pradeep-Kumar Hi. I am working on a project that I have to make a lineEdit which the user only allows to type numbers in it(not characters) . Also, I want to change the lineEdit's border color (If the user types a character, the color of the lineEdit's border is red and if the user types number, the color must be green). With the help of your code, I wrote the following code, but I got the error "F:\lineeditvalidator\mainwindow.cpp:42: error: expected type-specifier before 'QRegularExpressionValidator'
                    QValidator *validator = new QRegularExpressionValidator(regexp, this);
                    ^"
                    I will appreciate your help on this matter.

                    mainwindow.h:

                    #ifndef MAINWINDOW_H
                    #define MAINWINDOW_H
                    
                    #include <QMainWindow>
                    #include <QRegularExpression>
                    #include <QValidator>
                    #include <QRegularExpressionMatch>
                    #include <QDebug>
                    #include <QRegularExpressionValidator>
                    namespace Ui {
                    class MainWindow;
                    }
                    
                    class MainWindow : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        explicit MainWindow(QWidget *parent = 0);
                        ~MainWindow();
                    
                        void QRegularExpressionValidator();
                    
                    
                    private:
                        Ui::MainWindow *ui;
                    
                    public slots:
                        void getLineEditValue(QString stringValue);
                    
                    };
                    
                    #endif // MAINWINDOW_H
                    

                    main.cpp:

                    #include "mainwindow.h"
                    #include <QApplication>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        MainWindow w;
                        w.show();
                    
                        return a.exec();
                    }
                    

                    mainwindow.cpp:

                    #include "mainwindow.h"
                    #include "ui_mainwindow.h"
                    #include <QRegularExpression>
                    #include <QValidator>
                    #include <QRegularExpressionMatch>
                    #include <QDebug>
                    #include <QRegularExpressionValidator>
                    
                    MainWindow::MainWindow(QWidget *parent) :
                        QMainWindow(parent),
                        ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    
                        
                        connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);
                    
                    
                     }
                    
                    MainWindow::~MainWindow()
                    {
                        delete ui;
                    }
                    
                    
                    void MainWindow::getLineEditValue(QString stringValue)
                    {
                    
                    
                        qDebug () << "1" << stringValue;
                    
                        QString temp = ui->lineEdit->text();
                    
                        QRegExp regexp("^[0-9A]*$");
                    
                        QValidator *validator = new QRegularExpressionValidator(regexp, this);
                        ui->lineEdit->setValidator(validator);
                    
                        if (!regexp.exactMatch(temp))
                         {
                            qDebug () << "colour changed to red" <<  stringValue;
                             ui->lineEdit->setStyleSheet("border: 5px solid red");}
                    
                        else
                        {
                            qDebug () << "colour changed to green" <<  stringValue;
                            ui->lineEdit->setStyleSheet("border: 5px solid green");
                    
                        }
                    
                    }
                    
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @nanor
                    You would have been better creating a new topic, and perhaps referencing this one, than putting your new question in this old thread.

                    Anyway, this wrong: QRegExp regexp("^[0-9A]*$");. Your code should not include any mention of QRegExp from old Qt. You probably need: QRegularExpression regexp("^[0-9A]*$");, like posts above show.

                    N 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @nanor
                      You would have been better creating a new topic, and perhaps referencing this one, than putting your new question in this old thread.

                      Anyway, this wrong: QRegExp regexp("^[0-9A]*$");. Your code should not include any mention of QRegExp from old Qt. You probably need: QRegularExpression regexp("^[0-9A]*$");, like posts above show.

                      N Offline
                      N Offline
                      nanor
                      wrote on last edited by
                      #10

                      @JonB Thank you for replying. I have tried using QRegularExpression regexp("^[0-9A]*$") before; but I have got the error : ""F:\lineeditvalidator\mainwindow.cpp:52: error: 'class QRegularExpression' has no member named 'exactMatch'
                      if (!regexp.exactMatch(temp))"".

                      Sure.I will create a new topic .

                      JonBJ 1 Reply Last reply
                      0
                      • N nanor

                        @JonB Thank you for replying. I have tried using QRegularExpression regexp("^[0-9A]*$") before; but I have got the error : ""F:\lineeditvalidator\mainwindow.cpp:52: error: 'class QRegularExpression' has no member named 'exactMatch'
                        if (!regexp.exactMatch(temp))"".

                        Sure.I will create a new topic .

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #11

                        @nanor said in How to get characters which user is typing in QLineEdit ?:

                        if (!regexp.exactMatch(temp))"".

                        Yes, because now it's a QRegularExpression instead of a QRegExp you need to checkout the documentation for equivalent methods, e.g. https://doc.qt.io/qt-5/qregularexpression.html#match.

                        N 1 Reply Last reply
                        1
                        • JonBJ JonB

                          @nanor said in How to get characters which user is typing in QLineEdit ?:

                          if (!regexp.exactMatch(temp))"".

                          Yes, because now it's a QRegularExpression instead of a QRegExp you need to checkout the documentation for equivalent methods, e.g. https://doc.qt.io/qt-5/qregularexpression.html#match.

                          N Offline
                          N Offline
                          nanor
                          wrote on last edited by
                          #12

                          @JonB Thank you for mentioning the version. I changed QRegularExpressionValidator to QRegExpValidator and the code worked well (The user is allowed only to type a number and when a number is being typed in the lineEdit, the border color changes to green). The only part that does not still work is the border color changing to red , when I type a character.
                          I would appreciate if you help me on this matter.

                          Here is the updated cpp file:

                          #include "mainwindow.h"
                          #include "ui_mainwindow.h"
                          #include <QRegularExpression>
                          #include <QValidator>
                          #include <QRegularExpressionMatch>
                          #include <QDebug>
                          #include <QRegularExpressionValidator>
                          #include <QRegExpValidator>
                          
                          MainWindow::MainWindow(QWidget *parent) :
                              QMainWindow(parent),
                              ui(new Ui::MainWindow)
                          {
                              ui->setupUi(this);
                          
                          
                          
                             QRegExp regexp("^[Z0-9]*$");
                             QRegExpValidator *validator = new QRegExpValidator(regexp, this);
                             ui->lineEdit->setValidator(validator);
                          
                              connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);
                          
                          
                           }
                          
                          MainWindow::~MainWindow()
                          {
                              delete ui;
                          }
                          
                          
                          void MainWindow::getLineEditValue(QString stringValue)
                          {
                          
                              QRegExp regexp("^[Z0-9]*$");
                              qDebug () << "1" << stringValue;
                          
                              QString temp = ui->lineEdit->text();
                          
                          
                              if (!regexp.exactMatch(temp))
                               {
                                  qDebug () << "colour changed to red" <<  stringValue;
                                  ui->lineEdit->setStyleSheet("border: 5px solid red");
                          
                              }
                          
                              else
                              {
                                  qDebug () << "colour changed to green" <<  stringValue;
                                  ui->lineEdit->setStyleSheet("border: 5px solid green");
                          
                              }
                          
                          }
                          
                          
                          JonBJ 1 Reply Last reply
                          0
                          • N nanor

                            @JonB Thank you for mentioning the version. I changed QRegularExpressionValidator to QRegExpValidator and the code worked well (The user is allowed only to type a number and when a number is being typed in the lineEdit, the border color changes to green). The only part that does not still work is the border color changing to red , when I type a character.
                            I would appreciate if you help me on this matter.

                            Here is the updated cpp file:

                            #include "mainwindow.h"
                            #include "ui_mainwindow.h"
                            #include <QRegularExpression>
                            #include <QValidator>
                            #include <QRegularExpressionMatch>
                            #include <QDebug>
                            #include <QRegularExpressionValidator>
                            #include <QRegExpValidator>
                            
                            MainWindow::MainWindow(QWidget *parent) :
                                QMainWindow(parent),
                                ui(new Ui::MainWindow)
                            {
                                ui->setupUi(this);
                            
                            
                            
                               QRegExp regexp("^[Z0-9]*$");
                               QRegExpValidator *validator = new QRegExpValidator(regexp, this);
                               ui->lineEdit->setValidator(validator);
                            
                                connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(getLineEditValue(QString)),Qt::UniqueConnection);
                            
                            
                             }
                            
                            MainWindow::~MainWindow()
                            {
                                delete ui;
                            }
                            
                            
                            void MainWindow::getLineEditValue(QString stringValue)
                            {
                            
                                QRegExp regexp("^[Z0-9]*$");
                                qDebug () << "1" << stringValue;
                            
                                QString temp = ui->lineEdit->text();
                            
                            
                                if (!regexp.exactMatch(temp))
                                 {
                                    qDebug () << "colour changed to red" <<  stringValue;
                                    ui->lineEdit->setStyleSheet("border: 5px solid red");
                            
                                }
                            
                                else
                                {
                                    qDebug () << "colour changed to green" <<  stringValue;
                                    ui->lineEdit->setStyleSheet("border: 5px solid green");
                            
                                }
                            
                            }
                            
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @nanor
                            I wrote earlier:

                            Your code should not include any mention of QRegExp from old Qt.

                            and told you to use QRegularExpression, only. Yet your new code is even worse, you seem to have done exactly the opposite, full of more & more QRegExp stuff.....

                            Your CSS/QSS looks OK to me. What qDebug() lines do you receive? What character(s) do you type for what qDebug()/colour behaviour?

                            N 1 Reply Last reply
                            1
                            • JonBJ JonB

                              @nanor
                              I wrote earlier:

                              Your code should not include any mention of QRegExp from old Qt.

                              and told you to use QRegularExpression, only. Yet your new code is even worse, you seem to have done exactly the opposite, full of more & more QRegExp stuff.....

                              Your CSS/QSS looks OK to me. What qDebug() lines do you receive? What character(s) do you type for what qDebug()/colour behaviour?

                              N Offline
                              N Offline
                              nanor
                              wrote on last edited by
                              #14

                              @JonB I recieve only qDebug () << "colour changed to green" << stringValue;

                              I type letter(alphabet) for qDebug () << "colour changed to red" << stringValue; and type numbers for
                              qDebug () << "colour changed to green" << stringValue; .

                              JonBJ 1 Reply Last reply
                              0
                              • N nanor

                                @JonB I recieve only qDebug () << "colour changed to green" << stringValue;

                                I type letter(alphabet) for qDebug () << "colour changed to red" << stringValue; and type numbers for
                                qDebug () << "colour changed to green" << stringValue; .

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @nanor
                                Like I said, first change all your code from QRegExp... to QRegularExpression..., I have no idea how regexp.exactMatch(temp) works....

                                N 1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @nanor
                                  Like I said, first change all your code from QRegExp... to QRegularExpression..., I have no idea how regexp.exactMatch(temp) works....

                                  N Offline
                                  N Offline
                                  nanor
                                  wrote on last edited by
                                  #16

                                  @JonB Sure. I will change the code as you said. Thank you so much.

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved