SceneModifier
继承自QObject
,但尚未实现该keyPressEvent
方法。
keyPressEven
t 方法属于 windows,在这种情况下,Qt3DWindow
因此我们创建一个继承自它的类并实现该keyPressEvent
方法。
my3dwindow.h
#ifndef MY3DWINDOW_H
#define MY3DWINDOW_H
#include <Qt3DExtras/Qt3DWindow>
class My3DWindow: public Qt3DExtras::Qt3DWindow
{
Q_OBJECT
public:
My3DWindow(QScreen *screen = nullptr);
~My3DWindow();
protected:
void keyPressEvent(QKeyEvent *ev);
};
#endif // MY3DWINDOW_H
my3dwindow.cpp
#include "my3dwindow.h"
#include <QDebug>
#include <QKeyEvent>
My3DWindow::My3DWindow(QScreen *screen):Qt3DExtras::Qt3DWindow(screen)
{
}
My3DWindow::~My3DWindow()
{
}
void My3DWindow::keyPressEvent(QKeyEvent *ev)
{
switch (ev->key()) {
case Qt::Key_W:
qDebug()<<"Key is pressed";
break;
default:
break;
}
}
改变:
Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();
至:
My3DWindow *view = new My3DWindow();
主文件
[...]
#include "my3dwindow.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
My3DWindow *view = new My3DWindow();
view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
[...]
完整代码