Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Simplest way to get mouse events on Qt3DWindow



  • I want to get mouse events (like mouse position) on a Qt3D Window, every time I click inside the window.

    I've seen this topic

    https://forum.qt.io/topic/75730/mouse-controls-over-qt-3d-window/2

    but my Qt3DWindow is not inside any widget, so I don't think I need an EventFilter.

    I'm just beggining to learn C++ and Qt, so I'm trying to make the simplest program possible. In the code below (all my program is in this code), I would like to get the mouse position every time I click inside the Qt3D Window, but I can't even get a debug message every time I click.

    As far as I understand, the mouseMoveEvent function is only called one time, when the program gets executed. How would I call this function in the main loop, if there is such a thing in Qt?

    Do I need to do something like Qt3DInput::QMouseDevice *mouse = new Qt3DInput::QMouseDevice(scene);? But how would I use it?

    #include <QGuiApplication>
    
    #include <Qt3DCore/QEntity>
    #include <Qt3DRender/QCamera>
    #include <Qt3DRender/QCameraLens>
    #include <Qt3DCore/QTransform>
    #include <Qt3DCore/QAspectEngine>
    
    #include <Qt3DInput/QInputAspect>
    
    #include <Qt3DRender/QRenderAspect>
    #include <Qt3DExtras/QForwardRenderer>
    #include <Qt3DExtras/QPhongMaterial>
    #include <Qt3DExtras/QGoochMaterial>
    #include <Qt3DExtras/QSphereMesh>
    #include <Qt3DExtras/QCuboidMesh>
    
    #include <QMouseEvent>
    #include <Qt3DInput/QMouseDevice>
    #include <Qt3DInput/QMouseHandler>
    #include <Qt3DInput/QMouseEvent>
    
    #include <QDebug>
    
    #include "qt3dwindow.h"
    
    void mouseMoveEvent(Qt3DInput::QMouseEvent *event);
    
    Qt3DCore::QEntity *createScene()
    {
        // Root entity
        Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;
    
        // Material
        //Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
        Qt3DRender::QMaterial *material = new Qt3DExtras::QGoochMaterial(rootEntity);
    
        //Cube
        Qt3DCore::QEntity *cubeEntity = new Qt3DCore::QEntity(rootEntity);
        Qt3DExtras::QCuboidMesh *cubeMesh = new Qt3DExtras::QCuboidMesh;
    
        cubeEntity->addComponent(cubeMesh);
        cubeEntity->addComponent(material);
    
        return rootEntity;
    }
    
    int main(int argc, char* argv[])
    {
        QGuiApplication app(argc, argv);
        Qt3DExtras::Qt3DWindow view;
    
        Qt3DCore::QEntity *scene = createScene();
    
        // Camera
        Qt3DRender::QCamera *camera = view.camera();
        camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
        camera->setPosition(QVector3D(5.0, 5.0, 5.0f));
        camera->setViewCenter(QVector3D(0, 0, 0));
    
        Qt3DInput::QMouseEvent *e;
    
        mouseMoveEvent(e);
    
        view.setRootEntity(scene);
        view.show();
    
        return app.exec();
    }
    
    void mouseMoveEvent(Qt3DInput::QMouseEvent *event)
    {
        if (event->button() == Qt::LeftButton)
        {
            qDebug() << "ok";
        }
    }
    
    
    
    

Log in to reply