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.3k 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.
  • 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