Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simplest way to get mouse events on Qt3DWindow
Qt 6.11 is out! See what's new in the release blog

Simplest way to get mouse events on Qt3DWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saitodepaula
    wrote on last edited by Saitodepaula
    #1

    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";
        }
    }
    
    
    
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved