PDA

View Full Version : Enter causes cursor movement like in Tab Order



lotek
18th July 2011, 12:38
Hello,

In a dialog there are some edit fields e.g. QLineEdit. It is possible to set the Tab order, so that the cursor moves from one field to another when the Tab button is pressed.
Is it possible to achieve the same effect, when the return button is pressed?

Thx for your hints

mcosta
19th July 2011, 08:36
For the first question the answer is
"use QWidget::setTabOrder" or Designer

For the second you have to reimplement the QWidget::keyPressEvent and customize the default behaviour in case of Qt::Key_Enter or Qt::Key_Return

for example



void MyDialog::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
{
QWidget* w = focusWidget();
QWidget* next = 0;
if (w == ui->lineEdit) {
next = ui->lineEdit_2;
}
else if (w == ui->lineEdit_2) {
next = ui->lineEdit_3;
}
else if (w == ui->lineEdit_3) {
next = ui->buttonBox;
}
if (next)
next->setFocus();
}
break;

default:
QDialog::keyPressEvent(e);
}
}

lotek
20th July 2011, 11:23
For the first question the answer is
"use QWidget::setTabOrder" or Designer

For the second you have to reimplement the QWidget::keyPressEvent and customize the default behaviour in case of Qt::Key_Enter or Qt::Key_Return

for example



void MyDialog::keyPressEvent(QKeyEvent *e)
{
switch (e->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
{
QWidget* w = focusWidget();
QWidget* next = 0;
if (w == ui->lineEdit) {
next = ui->lineEdit_2;
}
else if (w == ui->lineEdit_2) {
next = ui->lineEdit_3;
}
else if (w == ui->lineEdit_3) {
next = ui->buttonBox;
}
if (next)
next->setFocus();
}
break;

default:
QDialog::keyPressEvent(e);
}
}


Thank you for the answer, but I am afraid it's not that easy with the keyPressEvent.
What about disabled fields (in different combinations)? The setTabOrder solves this automatically.
Of course I can imagine how to solve this, but I am looking for a QT mechanism that helps to fix such an issue.

Santosh Reddy
20th July 2011, 11:48
As an alternate solution, you can capture the KeyPressEvent, either using the above method or using a event filter and send a tab key event to the parent widget (i.e. converting a enter key event on a widget into a tab key event on the parent widget). This solution will respect the tab order, and is scalable solution, i.e. will work even if you changed tab order, or add more widgets in future.

As always, there are many ways to do a given job :)

mcosta
20th July 2011, 13:32
You can also use QWidget::focusNextPrevChild like



void MyDialog::keyPressEvent(QKeyEvent *e)
{
switch (e->key ()) {
case Qt::Key_Return:
case Qt::Key_Enter:
focusNextPrevChild (true);
break;

default:
QDialog::keyPressEvent (e);
}
}


or



void MyDialog::keyPressEvent(QKeyEvent *e)
{
switch (e->key ()) {
case Qt::Key_Return:
case Qt::Key_Enter:
{
QKeyEvent* newEvent = new QKeyEvent (QEvent::KeyPress, Qt::Key_Tab, e->modifiers ());
qApp->postEvent (this, newEvent, 0);
}
break;

default:
QDialog::keyPressEvent (e);
}
}