Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to catch enter key: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Remove non-functioning "toc" command)
(Cleanup)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{LangSwitch}}
 
[[Category:HowTo]]
[[Category:HowTo]]
'''English''' [[How_to_catch_enter_key_German|Deutsch]]
= How to catch enter key events =
== Overview ==
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.
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.


Line 16: Line 7:
Fortunately, Qt allows to reimplement the general event catching method. You need a new class with a method like this:
Fortunately, Qt allows to reimplement the general event catching method. You need a new class with a method like this:
<code>
<code>
bool eventFilter(QObject *obj, QEvent *event);
bool eventFilter(QObject* obj, QEvent* event);
</code>
</code>


Line 23: Line 14:
class keyEnterReceiver : public QObject
class keyEnterReceiver : public QObject
{
{
Q_OBJECT
    Q_OBJECT
 
protected:
protected:
bool eventFilter(QObject *obj, QEvent *event);
    bool eventFilter(QObject* obj, QEvent* event);
};
};
</code>
</code>
Line 32: Line 22:
Now, we have to implement the method:
Now, we have to implement the method:
<code>
<code>
bool keyEnterReceiver::eventFilter(QObject *obj, QEvent *event)
bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
{
{
if(event->type() == QEvent::KeyPress)
    if (event->type()==QEvent::KeyPress) {
{
        QKeyEvent* key = static_cast<QKeyEvent*>(event);
QKeyEvent '''key = static_cast<QKeyEvent'''>(event);
        if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
 
            //Enter or return was pressed
if((key->key() Qt::Key_Enter) || (key->key() Qt::Key_Return))
        } else {
{
            return QObject::eventFilter(obj, event);
//Enter or return was pressed
        }
}
        return true;
else
    } else {
{
        return QObject::eventFilter(obj, event);
return QObject::eventFilter(obj, event);
    }
}
    return false;
return true;
}
else
{
return QObject::eventFilter(obj, event);
}
 
return false;
}
}
</code>
</code>
Line 63: Line 45:
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:
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:
<code>
<code>
bool keyEnterReceiver::eventFilter(QObject *obj, QEvent *event)
bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
{
{
if(event->type() == QEvent::KeyPress)
    if(event->type()==QEvent::KeyPress) {
{
       
    } else {
}
        return QObject::eventFilter(obj, event);
else
    }
{
    return false;
return QObject::eventFilter(obj, event);
}
 
return false;
}
}
</code>
</code>
Line 82: Line 60:
We got a QEvent as a parameter. To read out which key was pressed, we need to convert the QEvent to a QKeyEvent:
We got a QEvent as a parameter. To read out which key was pressed, we need to convert the QEvent to a QKeyEvent:
<code>
<code>
QKeyEvent '''key = static_cast<QKeyEvent'''>(event);
QKeyEvent* key = static_cast<QKeyEvent*>(event);
</code>
</code>


Line 89: Line 67:
That's it. Now we only have to check whether it was "our" enter key or another key we are not interested in:
That's it. Now we only have to check whether it was "our" enter key or another key we are not interested in:
<code>
<code>
if((key->key() Qt::Key_Enter) || (key->key() Qt::Key_Return))
if ( (key->key()==Qt::Key_Enter) || (key->key()==Qt::Key_Return) ) {
{
    //Enter or return was pressed
//Enter or return was pressed
} else {
}
    return QObject::eventFilter(obj, event);
else
{
return QObject::eventFilter(obj, event);
}
}
return true;
return true;
Line 102: Line 77:
Finally, we can install our event handler:
Finally, we can install our event handler:
<code>
<code>
keyEnterReceiver *key = new keyEnterReceiver();
keyEnterReceiver* key = new keyEnterReceiver();
aWidgetInAnotherClass->installEventFilter(key);
aWidgetInAnotherClass->installEventFilter(key);

Revision as of 23:44, 27 June 2015

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);