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.
-
- 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);- 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 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"); } }
-
@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 ofQRegExp
from old Qt. You probably need:QRegularExpression regexp("^[0-9A]*$");
, like posts above show. -
@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 .
-
@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 aQRegExp
you need to checkout the documentation for equivalent methods, e.g. https://doc.qt.io/qt-5/qregularexpression.html#match. -
@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"); } }
-
@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 & moreQRegExp
stuff.....Your CSS/QSS looks OK to me. What
qDebug()
lines do you receive? What character(s) do you type for whatqDebug()
/colour behaviour?