0

我在尝试捕获击键的 qt 程序中遇到错误。在我的 qt 程序的 keyPressedEvent 函数中,但我遇到了一个奇怪的错误:

frenzywindow.cpp:16:50: error: no 'void FrenzyWindow::keyPressEvent(QKeyEvent*)' member function declared in class 'FrenzyWindow'
make: *** [frenzywindow.o] Error 1

该类扩展了 qmainwindow

这是我的头文件:

#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

这是我的 cpp 文件:

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

    }
}
4

1 回答 1

2

你读过编译器错误吗?这正是问题所在。您需要keyPressEvent在头文件中定义。

protected:
    void keyPressEvent(QKeyEvent *event);
于 2012-12-13T00:40:51.850 回答