Ana
Ana

Reputation: 3

Qt keyPressEvent Error

I wrote this piece of code, but I get this error:

"left of '->key' must point to class/struct/union/generic type".

Considering that GUI is a class derived from the QWidget class, how can I properly catch the pressing of a key?

void GUI::keyPressEvent(QKeyEvent *event)
{
    if (event->key()==Qt::Key_Up) {
        //do something
    }
}

The keyPressEvent is declared like this:

protected:    
    virtual void keyPressEvent(QKeyEvent *event);

Upvotes: 0

Views: 759

Answers (1)

deW1
deW1

Reputation: 5660

You have to include

#include <QKeyEvent>

then it should work as intended.


Apart from that you should use the Q_DECL_OVERRIDE macro like:

protected:    
    virtual void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;

As @KubaOber mentioned, if you use a C++ Compiler supporting >=C++11 either by default or by you activating it with CONFIG += c++11 then you can use the override keyword instead.

Upvotes: 1

Related Questions