SceneModifier
繼承,也沒有實現的方法keyPressEvent
。
keyPressEven
t方法屬於窗口,在這種情況下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();
的main.cpp
[...]
#include "my3dwindow.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
My3DWindow *view = new My3DWindow();
view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
[...]
Complete code
'SceneModifier'從'QObject'繼承,也沒有實施'keyPressEvent'方法 – eyllanesc