How to get characters which user is typing in QLineEdit ?



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



  • May InputMask help?

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



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



  • 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);
    }



  • 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 @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.



    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);
    }
    

    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.