Mouse controls over Qt 3D Window



  • I have a QWidget containing a Qt3DWindow(). I'd like to be able to "zoom" in and out on a QtEntity, within the Qt3DWindow, using the mouse scroll wheel, while hovering over the window.

    I have the functionality working, but only when hovering the mouse outside of the Qt3DWindow frame. Here's my code for initializing the window and processing mouse wheel events.

    Window Initialization:

    mainView = new Qt3DExtras::Qt3DWindow();
    mainView->defaultFramegraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
    
    QWidget *container = QWidget::createWindowContainer(mainView);
    

    Processing wheel events:

    void ModelView::wheelEvent(QWheelEvent *event){
    
        QVector3D vec;
    
        vec = cameraEntity->position() - modifier->m_transform->translation();
    
        vec = vec.normalized();
    
        QPoint delta = event->angleDelta();
    
        int zoom_distance = delta.y()*0.01;
    
        vec = cameraEntity->position() - zoom_distance*vec;
    
        cameraEntity->setPosition(vec);
    }
    

    What's the trick for overriding the window's mouse grab when hovering over the Qt3DWindow frame?

    Thanks in advance for any help.


  • Qt Champions 2016

    You need a mouse controller in Qt3D, not an override of the view's mouse handling routines. Take a look at the Qt3DInput's module docs, for example here's a place to start.



  • @kshegunov I ended up using an event filter to catch the mouse wheel events over the window.

        mainView = new Qt3DExtras::Qt3DWindow();
        mainView->defaultFramegraph()->setClearColor(QColor(QRgb(0x4d4d4f)));
    
        mainView->installEventFilter(this);
    
    bool ModelView::eventFilter(QObject *obj, QEvent *event)
    {
        switch(event->type())
        {
        case QEvent::KeyPress:
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            return true;
        }
            break;
        case QEvent::Wheel:
        {
            QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
    
            QVector3D vec;
    
            vec = cameraEntity->position() - modifier->m_transform->translation();
    
            vec = vec.normalized();
    
            QPoint delta = wheelEvent->angleDelta();
    
            int zoom_distance = delta.y()*0.01;
    
            vec = cameraEntity->position() - zoom_distance*vec;
    
            cameraEntity->setPosition(vec);
            return true;
        }
            break;
        default:
            // standard event processing
            return QObject::eventFilter(obj, event);
            break;
    
        }
    
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.