user152508
user152508

Reputation: 3141

Immediate validation and editingFinished signal

I want to do immediate validation of my edit lines. I want to tell the user immediately after he left the edit line field that the value he entered is maybe incorrect.

So I connect the line edit with editingFinished() signal. However the problem is that the signal is not emitted when I click outside of the line edit, for example when I just click on the dialog. It is emitted when I click on some other line edit on the dialog. So I am looking for signal that is emitted everytime I click outside the edit line. How can I achieve this ?

connect(mLineEdit, SIGNAL(editingFinished()), this, SLOT(Validate()))

MyDlg::Validate()
{
     QString text = mLineEdit->text();
     bool isValid = check_if_valid(text);
     if(!isValid)
         // set the color of edit line to be red        
}

Upvotes: 1

Views: 1383

Answers (1)

RA.
RA.

Reputation: 7777

Firstly, consider using a QValidator implementation with your line edit to perform validation (QIntValidator, QDoubleValidator, or QRegExpValidator). You can install the validator using QLineEdit::setValidator(QValidator*). If one of these validators doesn't work for you, it's probably wise to look into writing your own validator by subclassing QValidator and providing an implementation of validate, then installing your own custom validator on the line edit.

Failing all of that, you can proceed as you have above, but you'll need to listen to either the textChanged(const QString&) signal or the textEdited(const QString&) signal (the former is emitted even when the line edit text is changed programatically, whereas the latter is not).

Upvotes: 2

Related Questions