键按下事件错误 Qt 实现函数时



>我在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;
    }
}

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

protected:
    void keyPressEvent(QKeyEvent *event);

相关内容

  • 没有找到相关文章

最新更新