PDA

View Full Version : keypressEventFilter() to accept Enter and Return Tab



ad5xj
27th July 2007, 19:09
I have a keypressEventFilter on a textedit box in order to trap the Return or Enter key press. This part works great. I then wish to continue the event returning a Tab instead.

The documentation talks about it -
"(Sometimes in data-entry applications Enter does the same as Tab; this can easily be achieved in Qt by implementing an event filter.) " ,

but there is no example or other documentation to explain how to change the the key press from Qt::Key_Enter to Qt::Key_Tab. There is only the generic "// special tab handling here" comment where code should be to illustrate what the text has already given as an example.

I thought I could return the Qt:Key_Tab value but the return must be an event not a value. I don't think issuing another keypress with the QKeyPress or QEvent is right, so what is appropriate for this situation?

jpn
27th July 2007, 19:39
Perhaps I overlooked something but I think you might have just found a bug in the docs. The described behaviour is fairly possible with QWidget::focusNextPrevChild() BUT it's protected so actually one needs to reimplement the key press event handler, not to install an event filter:


myTextEdit->setTabChangesFocus(true);

void MyTextEdit::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Return)
{
focusNextPrevChild(true);
}
else
{
QTextEdit::keyPressEvent(event);
}
}

ad5xj
27th July 2007, 20:30
Implementing your code in this way is not the whole story is it? I mean this code:

void txtName::keyPressEvent(QKeyEvent* event)
{
if (event->key() == Qt::Key_Return))
{
focusNextPrevChild(true);
}
else
{
QTextEdit::keyPressEvent(event);
}
}


causes an error because it does not know that txtName is an object on my MainWindow. - "Error:Undeclared first use". Plus the focusNextPrevChild(true) statement draws an error that is similar. What am I missing?

jpn
27th July 2007, 20:32
What you need is a QTextEdit subclass:


class MyTextEdit : public QTextEdit
{
public:
...
protected:
void keyPressEvent(QKeyEvent* event);
...
};

ad5xj
28th July 2007, 17:42
Ok, the TextEdit is subclassed and now it does not show up on my MainForm. It is in a QFrame container. If I issue a show() it is a separate window and the setTabOrder() gives a debug message about two different windows. Whatz up with that?

marcel
28th July 2007, 19:14
I don't know. Probably something wrong in other part of the code. Can you post it?

joshlareau
30th July 2007, 16:20
Has anyone considered using the QShortcut class to handle intercepting keyboard shortcuts? I had a table widget that was "stealing" the enter and tab keys from me and I got around the problem by using QShortCut as follows:

//Intercept the CTRL-TAB key from the table widget
QShortcut* shortcutTabKey;
shortcutTabKey = new QShortcut( QKeySequence( tr("Ctrl+TAB") ), m_tableWidget );
connect( shortcutTabKey, SIGNAL( activated() ), this, SLOT( OnTableShortCutTabKey() ) );

Hopefully this helps someone, as it seems easier than overriding QWidget::event.

macias
31st July 2007, 16:45
The documentation talks about it -
"(Sometimes in data-entry applications Enter does the same as Tab; this can easily be achieved in Qt by implementing an event filter.) " ,


This is quite opposite to what you want to achieve. In your case you have to kill filter because it is already installed -- read about tab handling, it is a special key for focus switching.

have a nice day, bye

Michiel
31st July 2007, 17:18
It might be as simple as this (event filter function frame taken from the docs):


bool FilterObject::eventFilter(QObject *object, QEvent *event)
{
if (object == target && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Enter) {
*keyEvent = QKeyEvent(Qt::Key_Tab);
}
}
return false;
}

Seems that the QKeyEvent lacks a setKey() function. But I'd think the event parameter is not non-const for nothing.