PDA

View Full Version : Disable Tab Key



otortos
22nd March 2006, 17:18
Somebody knows how to disable the tab key. I need to disable the use of tab key to move between fileds.

Thanks for your help

high_flyer
22nd March 2006, 21:12
void MyWidget::keyPressEvent(QKeyEvent *e)
{
if(e->key()==Qt::Key_Tab)
{
e->accept();
//do somthing if you want
}
else e->reject();

}

otortos
23rd March 2006, 17:52
I have this error with your code:

error: ‘class QKeyEvent’ has no member named ‘reject’

I am using QT 3.3.6

oob2
23rd March 2006, 18:28
You can try turn of TabFocus for each fields.

For example:
If you have one QPushButton and one QLineEdit in your window. You can try

mMyBtn->setFocusPolicy(QWidget::NoFocus);
mMyEdit->setFocusPolicy(QWidget::NoFocus);

high_flyer
23rd March 2006, 20:06
I have this error with your code:

error: ‘class QKeyEvent’ has no member named ‘reject’

I am using QT 3.3.6
Sorry, it should be e->ingnore();

otortos
24th March 2006, 20:37
This is my code:

void Form1::keyPressEvent(QKeyEvent *e)
{
if(e->key()==Qt::Key_Tab)
{
e->ignore();
}
else
{
e->ignore();
}
}

and, when i press "tab" still move to the next field.

jpn
25th March 2006, 16:27
This approach is wrong. The key press event goes directly to a focused child.

You could prevent the child from getting tab key press events by an event filter, but in my opinion that wouldn't be very good idea. I don't know what kind of widgets do you have, but some widgets have an "internal navigation" etc. with tab key. E.g. a date and time edit, which first changes the current field, and then after the last field changes the focus.


// install event filter for all widgets, e.g. in "MyWidget" constructor
QList<QWidget*> widgets = findChildren<QWidget*>();
foreach (QWidget* widget, widgets)
widget->installEventFilter(this);



bool MyWidget::eventFilter(QObject* o, QEvent* e)
{
if (e->type() == QEvent::KeyPress)
{
QKeyEvent* k = static_cast<QKeyEvent*>(e);
if (k->key() == Qt::Key_Tab)
{
// filter tab out
return true;
}
}
return false;
}


A better solution could be to trick the tab order. By this way you wouldn't prevent widgets from receiving and acting due to tab key press events.


QList<QWidget*> widgets = findChildren<QWidget*>();
foreach (QWidget* widget, widgets)
widget->setTabOrder(widget, widget);