Did you read the compiler error? That's exactly what the problem is. You need to define keyPressEvent
in your header file.
protected:
void keyPressEvent(QKeyEvent *event);
سؤال
I'm getting an error in a qt program where im trying to capture keystrokes. In the keyPressedEvent function in my qt program but i'm geting a weird error:
frenzywindow.cpp:16:50: error: no 'void FrenzyWindow::keyPressEvent(QKeyEvent*)' member function declared in class 'FrenzyWindow'
make: *** [frenzywindow.o] Error 1
The class extends qmainwindow
heres my header file:
#ifndef FRENZYWINDOW_H
#define FRENZYWINDOW_H
#include <QMainWindow>
#include "frenzy.h"
namespace Ui {
class FrenzyWindow;
}
class FrenzyWindow : public QMainWindow
{
Q_OBJECT
public:
explicit FrenzyWindow(QWidget *parent = 0);
~FrenzyWindow();
signals:
void moveUp();
void moveDown();
void moveLeft();
void moveRight();
private:
Ui::FrenzyWindow *ui;
Frenzy f;
};
#endif // FRENZYWINDOW_H
here is my cpp file:
#include "frenzywindow.h"
#include "ui_frenzywindow.h"
FrenzyWindow::FrenzyWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::FrenzyWindow)
{
ui->setupUi(this);
}
FrenzyWindow::~FrenzyWindow()
{
delete ui;
}
void FrenzyWindow::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
case Qt::UpArrow:
emit moveUp();
break;
case Qt::DownArrow:
emit moveDown();
break;
case Qt::LeftArrow:
emit moveLeft();
break;
case Qt::RightArrow:
emit moveRight();
break;
default:
event->ignore();
break;
}
}
المحلول
Did you read the compiler error? That's exactly what the problem is. You need to define keyPressEvent
in your header file.
protected:
void keyPressEvent(QKeyEvent *event);