PDA

View Full Version : Clear focus on editingFinished



iwatsu
6th October 2011, 17:09
I thought that when pressing the enter key on a QLineEdit, it would remove the focus (and the cursor wouldn't be visible anymore).
For some reason it doesn't behave that way in this simple example. Can somebody explain why?



LineEditTest::LineEditTest(QWidget *parent)
: QWidget(parent)
{
QLineEdit *edit = new QLineEdit;
Q_ASSERT(connect(edit, SIGNAL(editingFinished()), this, SLOT(SlotTextEdited())));

QHBoxLayout *l = new QHBoxLayout;
l->addWidget(edit);
setLayout(l);
}


void LineEditTest::SlotTextEdited()
{
QLineEdit *lineEdit = dynamic_cast<QLineEdit*>(sender());
double value = lineEdit->text().toDouble();
...
// lineEdit->clearFocus(); // If this line is enabled the cursor disappears
}

wysota
7th October 2011, 20:41
Return/Enter doesn't influence focus unless it is programmed to do so. Tab is the key for changing focus. By the way, using editingFinished() is probably not the proper way of doing what you want. "returnPressed()" would be a much better choice to react on the return key by losing focus. Of course editingFinished() is a good place to react on changes to the text of the edit.