PDA

View Full Version : QLineEdit



Attila Kovács
22nd January 2017, 09:05
Hello

I have a QLineedit with mask and qvalidator (subclassed)

How can i prevent to move away the focus if the input isn't match the mask or validator ?

Because neither mask nor qvalidator don't prevent to move away focus from QLineEdit.

And editingfinished isn't work because :

"void QLineEdit::editingFinished()

This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable."

void MainWindow::on_lineEdit_editingFinished()
{
if (ui->lineEdit->text() != "1111") ui->lineEdit->setFocus();
}


So mask (an validator) doesn't work together with editingFinsihed signal.

thank you Attila

anda_skoa
22nd January 2017, 10:17
You should be able to detect the focus change by implementing the focusOutEvent() handler.

Cheers,
_

Attila Kovács
22nd January 2017, 16:27
Thank you Anda_skoa

But i'm a beginner
Could you give me a short sample

i have tried this , but the program crashed

bool MainWindow::eventFilter(QObject *filterObj, QEvent *event)
{
if (filterObj == ui->lineEdit ) {
if(event->type() == QEvent::FocusOut) {
if (ui->lineEdit->text() != "1111") { ui->lineEdit->setFocus();};
return true;
};
};
return false;
}








thank you Attila

anda_skoa
23rd January 2017, 09:33
Have you checked where it crashes?

Alternatively to an event filter you could also try reimplementing QWidget::focusOutEvent(), i.e. derive a new class from QLineEdit and overwrite the virtual method.

Cheers,
_

Attila Kovács
23rd January 2017, 10:58
Thnak you for your answer

the problem is at setFocus()

and if i reimplement the focusOutEvent - the result is the same.

Attila

Attila Kovács
23rd January 2017, 13:39
Similar problem like here :

https://forum.qt.io/topic/11423/forbid-leaving-a-qlineedit/6

Attila

anda_skoa
24th January 2017, 09:03
You could try calling setFocus() delayed since it is a slot



QMetaObject::invokeMethod(lineEdit, "setFocus", Qt::QueuedConnection);


Cheers,
_

Attila Kovács
24th January 2017, 10:55
thank you

but unfortunatelly i doesn't help me - the cursor bounce from one control to another and back again ....

Thank you your kindness
Attila