QLineEdit
has a signal QLineEdit::editingFinished
that gets emitted when the user finished editing, for example by pressing enter. However if a validator or an input mask was set, then editingFinished
only gets emitted if the input is valid.
But how can I react to the user finishing the editing regardless of the validity of the input? Do I have to manually check for enter, return, the widget losing focus, etc. ?
The reason for this: I wanted to create a custom widget for editing numbers using a QDoubleValidator
. When the user finishes editing and the input is invalid (wrong range, empty text, ...) I want to reset it to some valid default value. Something like this:
class NumberEdit: public QLineEdit
{
public:
NumberEdit(double min, double max)
{
setValidator(new QDoubleValidator(min, max, 10));
setText(QString::number(min));
connect(this, /* this is the problem */, [this, min]() {
if(!hasAcceptableInput())
setText(QString::number(min)); // Reset to valid number
});
}
};