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.
I'd recommend using an event filter to intercept the Qt3DWindow events. Your ModelView class can install itself as an event filter on the Qt3DWindow, detect wheel events, handle them itself, and return true to indicate that they're handled. For all other events, return false, and the Qt3DWindow will receive and process them normally.
Look at QObject::installEventfilter and QObject::eventFilter methods in the docs.