Reputation: 17223
I wanted to know if its possible for QValidator to show a Popup box incase a QlineEdit item is invalid. I know that Qvalidator simply filters the input and only displays the data if its valid. However I want it to display the data that I type in and display a tooltip (popup box) incase the input is invalid.
Upvotes: 2
Views: 1240
Reputation: 36630
You can subclass the specific validator you want to use and override its validate
method, so that it emits a signal after validation. For example, for a QIntValidator
, you could create a sub class as follows (code only showing the relevant excerpts):
Header file:
class QIntValidatorReporter : public QIntValidator {
Q_OBJECT;
public:
QIntValidatorReporter(int minimum, int maximum, QObject * parent = 0);
virtual QValidator::State validate(QString& input, int& pos ) const;
signals:
void setError(const QString& msg) const;
};
.cpp file:
QValidator::State QIntValidatorReporter::validate(QString& input, int& pos ) const {
QValidator::State result = QIntValidator::validate(input, pos);
if (result == QValidator::Invalid) {
emit setError(QString("Allowed range: %1 to %2").arg(bottom()).arg(top()));
} else {
emit setError("");
}
return result;
}
By connecting the setError
signal to any slot as required, you can implement your required functionality to report the validation state.
See https://github.com/afester/StackOverflow/tree/master/Qt/QValidator for a complete sample project.
Upvotes: 2