鼠标控制Qt 3D窗口 [英] Mouse controls over Qt 3D Window

查看:620
本文介绍了鼠标控制Qt 3D窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含Qt3DWindow()的QWidget.我希望能够在鼠标悬停在窗口上方的同时,使用鼠标滚轮在Qt3DWindow内的QtEntity上进行缩放".

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.

我可以使用该功能,但是仅当将鼠标悬停在Qt3DWindow框架之外时才可以.这是我的代码,用于初始化窗口和处理鼠标滚轮事件.

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.

窗口初始化:

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

QWidget *container = QWidget::createWindowContainer(mainView);

处理车轮事件:

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);
}

当悬停在Qt3DWindow框架上时,覆盖窗口的鼠标抓取的窍门是什么?

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

在此先感谢您的帮助.

推荐答案

我建议使用事件过滤器来拦截Qt3DWindow事件.您的ModelView类可以将自己作为事件过滤器安装在Qt3DWindow上,检测出滚轮事件,自行处理它们,然后返回true表示它们已得到处理.对于所有其他事件,返回false,Qt3DWindow将正常接收和处理它们.

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.

在文档中查看QObject :: installEventfilter和QObject :: eventFilter方法.

Look at QObject::installEventfilter and QObject::eventFilter methods in the docs.

这篇关于鼠标控制Qt 3D窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆