How to catch enter key: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
m (use syntaxhighlight)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
'''English''' [[How to catch enter key German|Deutsch]]
{{LangSwitch}}
[[Category:HowTo]]
There are many different situations where you can use the enter key, e.g. to start a search action. But implementing something like this is not that easy - Qt catches enter keys before you even get the event.


=How to catch enter key events=
== Solution ==


==Overview==
Fortunately, Qt allows to reimplement the general event catching method. You need a new class with a method like this:
<syntaxhighlight lang="cpp">
bool eventFilter(QObject* obj, QEvent* event);
</syntaxhighlight>


There are many different situations where you can use the enter key, e.g. to start a search action. But implementing something like this is not that easy – Qt catches enter keys before you even get the event.
That's everything:
<syntaxhighlight lang="cpp">
class keyEnterReceiver : public QObject
{
    Q_OBJECT
protected:
    bool eventFilter(QObject* obj, QEvent* event);
};
</syntaxhighlight>


==Solution==
Now, we have to implement the method:
<syntaxhighlight lang="cpp">
bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type()==QEvent::KeyPress) {
        QKeyEvent* key = static_cast<QKeyEvent*>(event);
        if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
            //Enter or return was pressed
        } else {
            return QObject::eventFilter(obj, event);
        }
        return true;
    } else {
        return QObject::eventFilter(obj, event);
    }
    return false;
}
</syntaxhighlight>


Fortunately, Qt allows to reimplement the general event catching method. You need a new class with a method like this:<br />
That was quiet fast - so here is a detailled explanation:


That’s everything:<br />
=== Key pressed? ===


Now, we have to implement the method:<br />
First, we check if any key was pressed. If not, it is a event that has nothing to do with keys - and Qt should handle it:
<syntaxhighlight lang="cpp">
bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
{
    if(event->type()==QEvent::KeyPress) {
        …
    } else {
        return QObject::eventFilter(obj, event);
    }
    return false;
}
</syntaxhighlight>


That was quiet fast – so here is a detailled explanation:
=== Convertion ===


===Key pressed?===
We got a QEvent as a parameter. To read out which key was pressed, we need to convert the QEvent to a QKeyEvent:
<syntaxhighlight lang="cpp">
QKeyEvent* key = static_cast<QKeyEvent*>(event);
</syntaxhighlight>


First, we check if any key was pressed. If not, it is a event that has nothing to do with keys – and Qt should handle it:<br />
=== Enter/Return or another key? ===


===Convertion===
That's it. Now we only have to check whether it was "our" enter key or another key we are not interested in:
<syntaxhighlight lang="cpp">
if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
    //Enter or return was pressed
} else {
    return QObject::eventFilter(obj, event);
}
return true;
</syntaxhighlight>


We got a QEvent as a parameter. To read out which key was pressed, we need to convert the QEvent to a QKeyEvent:<br />
Finally, we can install our event handler:
 
<syntaxhighlight lang="cpp">
===Enter/Return or another key?===
keyEnterReceiver* key = new keyEnterReceiver();
 
aWidgetInAnotherClass->installEventFilter(key);
That’s it. Now we only have to check whether it was “our” enter key or another key we are not interested in:<br />
</syntaxhighlight>
 
Finally, we can install our event handler:<br />
 
===Categories:===
 
* [[:Category:HowTo|HowTo]]

Latest revision as of 08:50, 25 May 2021

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

There are many different situations where you can use the enter key, e.g. to start a search action. But implementing something like this is not that easy - Qt catches enter keys before you even get the event.

Solution

Fortunately, Qt allows to reimplement the general event catching method. You need a new class with a method like this:

bool eventFilter(QObject* obj, QEvent* event);

That's everything:

class keyEnterReceiver : public QObject
{
    Q_OBJECT
protected:
    bool eventFilter(QObject* obj, QEvent* event);
};

Now, we have to implement the method:

bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type()==QEvent::KeyPress) {
        QKeyEvent* key = static_cast<QKeyEvent*>(event);
        if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
            //Enter or return was pressed
        } else {
            return QObject::eventFilter(obj, event);
        }
        return true;
    } else {
        return QObject::eventFilter(obj, event);
    }
    return false;
}

That was quiet fast - so here is a detailled explanation:

Key pressed?

First, we check if any key was pressed. If not, it is a event that has nothing to do with keys - and Qt should handle it:

bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
{
    if(event->type()==QEvent::KeyPress) {
        
    } else {
        return QObject::eventFilter(obj, event);
    }
    return false;
}

Convertion

We got a QEvent as a parameter. To read out which key was pressed, we need to convert the QEvent to a QKeyEvent:

QKeyEvent* key = static_cast<QKeyEvent*>(event);

Enter/Return or another key?

That's it. Now we only have to check whether it was "our" enter key or another key we are not interested in:

if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
    //Enter or return was pressed
} else {
    return QObject::eventFilter(obj, event);
}
return true;

Finally, we can install our event handler:

keyEnterReceiver* key = new keyEnterReceiver();
aWidgetInAnotherClass->installEventFilter(key);