Based on the official example given in Qt, I try to catch keys inputs using Qt3DInput::QKeyboardHandler.
So after the following piece of code:
Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
OrbitTransformController *controller = new OrbitTransformController(sphereTransform);
controller->setTarget(sphereTransform);
controller->setRadius(20.0f);
I add :
Qt3DInput::QKeyboardHandler *keyboardHandler = new Qt3DInput::QKeyboardHandler(rootEntity);
keyboardHandler->setSourceDevice(new Qt3DInput::QKeyboardDevice(rootEntity));
QObject::connect(keyboardHandler,
&Qt3DInput::QKeyboardHandler::pressed,
controller,
&::OrbitTransformController::onPressed);
rootEntity->addComponent(keyboardHandler);
But the signal is never caught or maybe ever sent. What am I missing ? Attaching my object to another Qt3DCore::QEntity doesn't fix anything.
The official complete example without my adds : https://doc.qt.io/qt-6/qt3d-simple-cpp-example.html
EDIT 1:
I add more important code snippets with my comments:
orbittransformcontroller.h :
class OrbitTransformController : public QObject
{
Q_OBJECT
Q_PROPERTY(Qt3DCore::QTransform* target READ target WRITE setTarget NOTIFY targetChanged)
Q_PROPERTY(float radius READ radius WRITE setRadius NOTIFY radiusChanged)
Q_PROPERTY(float angle READ angle WRITE setAngle NOTIFY angleChanged)
public:
OrbitTransformController(QObject *parent = 0);
...
public slots:
// Begin my Code
void onPressed(Qt3DInput::QKeyEvent *event);
// End my Code
...
};
orbittransformcontroller.cpp :
...
// Begin my Code
void OrbitTransformController::onPressed(Qt3DInput::QKeyEvent *event)
{
qDebug() << "OrbitTransformController::onPressed !";
}
// End my Code
the main.cpp :
Qt3DCore::QEntity *createScene()
{
// The original code :
...
Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
OrbitTransformController *controller = new OrbitTransformController(sphereTransform);
controller->setTarget(sphereTransform);
controller->setRadius(20.0f);
// Begin my Code
Qt3DInput::QKeyboardHandler *keyboardHandler = new Qt3DInput::QKeyboardHandler(rootEntity);
keyboardHandler->setSourceDevice(new Qt3DInput::QKeyboardDevice(rootEntity));
QObject::connect(keyboardHandler, &Qt3DInput::QKeyboardHandler::pressed, controller, &::OrbitTransformController::onPressed);
rootEntity->addComponent(keyboardHandler);
// End my Code
...
}
Nothing more ! You will have noticed that Qt3D documentation and examples are quite light :(