【问题标题】:keyPressedEvent error Qt when implementing function实现功能时keyPressedEvent错误Qt
【发布时间】:2012-11-30 19:54:53
【问题描述】:

我在尝试捕获击键的 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;

    }
}

【问题讨论】:

    标签: c++ qt keypress qmainwindow


    【解决方案1】:

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

    protected:
        void keyPressEvent(QKeyEvent *event);
    

    【讨论】:

    • 我又收到 3 个错误:frenzywindow.cpp:在成员函数 'virtual void FrenzyWindow::keyPressEvent(QKeyEvent*)'中:frenzywindow.cpp:19:17:错误:无效使用不完整类型' struct QKeyEvent' /usr/include/qt4/QtGui/qwidget.h:83:7: 错误:'struct QKeyEvent' frenzywindow.cpp:34:18 的前向声明:错误:不完整类型'struct QKeyEvent' /usr 的无效使用/include/qt4/QtGui/qwidget.h:83:7: 错误: 'struct QKeyEvent' 的前向声明 make: *** [frenzywindow.o] 错误 1
    • 您的 cpp 文件中需要一个 #include &lt;QKeyEvent&gt;
    • 如果我从声明和函数中删除事件,它会编译。但是我如何获得密钥呢?
    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 2020-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多