PDA

View Full Version : QLineEdit & keyPressEvent(QKeyEvent *)



munna
28th March 2006, 11:51
Hi,

I have reimplemented the keyPressEvent of QLineEdit ( Qt4). Following is the code
void LineEdit::keyPressEvent(QKeyEvent *e)
{
if(e->key() == Qt::Key_Backtab){
//Do something
}
else if(e->key() == Qt::Key_Return || e->key() == Qt::Key_Tab || e->key() == Qt::Key_Enter){
//Do something
}
else{
QLineEdit::keyPressEvent(e);
}
}

1. When I press the tab key this part of code is not executed. Another widget which is the part of my application gets the focus (Which should have happened if I wouldn't have reimplemented the keyPressEvent). How can i override this behaviour?

2. Is Qt::KeyBacktab same as Shift+Tab?

3. Can some please show me how to use modifiers. I am trying to use it in the following way :

if(e->key() == Qt::Key_Return && e->modifiers() == Qt::ShiftModifier){

}

Is it the correct way of using modifiers?

Thanks a lot.

wysota
28th March 2006, 13:45
AFAIR tab keys are not handled by keyPressEvents. You have to implement event() to catch them.

It's explained in the docs (http://doc.trolltech.com/4.1/eventsandfilters.html).

munna
29th March 2006, 11:56
Thanks for replying


bool LineEdit::event(QEvent *e)
{
if(e->type() == QEvent::KeyPress){
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if(ke->modifiers() & Qt::ShiftModifier){
if (ke->key() == Qt::Key_Tab) {
editMode->focusNextWidget();
return true;
}
}
else if (ke->key() == Qt::Key_Tab) {
editMode->focusNextWidget();
return true;
}
}
return QLineEdit::event(e);
}

In the above code I am trying to achieve the same functionality for both tab and shift+tab.
Tab works perfectly whereas shift+tab is not working.

Pls help
Thanks

zlatko
29th March 2006, 12:54
What ke->modifiers() return ?
Maiby try use QAccel?

munna
29th March 2006, 13:00
When i debug the program pointer gets into the first if condition but it is not getting into the second one



if(ke->modifiers() & Qt::ShiftModifier){
if (ke->key() == Qt::Key_Tab) {
editMode->focusNextWidget();
return true;
}
}

wysota
29th March 2006, 13:11
Shift+Tab is BackTab. Try it instead of that modifier.

BTW. Why are you reimplementing Tab to do the same it does by its own?

zlatko
29th March 2006, 13:12
Oh yes...it becouse first pressing its Shift. So you must catch 2 events : frist for Shift and second for Tab

munna
29th March 2006, 14:50
BTW. Why are you reimplementing Tab to do the same it does by its own?

I am yet to write the code for backtab. I just wanted to see how modifiers work before implementing the functionality, that is why i am calling the same function in the same place.

Thanks a lot