检测 3D 对象的触摸,除了鼠标点击
Detecting touch on 3D objects, in addition to mouse click
如何使用 Qt3DRender::QObjectPicker 触摸事件?
我正在通过这种方法将 Qt3DRender::QObjectPicker
组件添加到我的 Qt3D 实体中:
Qt3DRender::QObjectPicker *MyClass::createObjectPickerForEntity(Qt3DCore::QEntity *entity)
{
Qt3DRender::QObjectPicker *picker = new Qt3DRender::QObjectPicker(entity);
picker->setHoverEnabled(false);
entity->addComponent(picker);
connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &MyClass::handlePickerPress);
return picker;
}
我的对象选择器适用于鼠标点击,但不适用于触摸事件。有人知道如何在智能手机上使用 Qt3D 对象选择器和触摸事件吗?
@FlorianBlume 帮我解决了问题。可以使用 QScreenRayCaster
检测 Qt3D 实体上的触摸。我必须向我的根实体添加一个 QScreenRayCaster
组件:
/*
* You have to add the ray caster to the root entity as a component
* Perform ray casting tests by specifying "touch" coordinates in screen space
*/
m_screenRayCaster = new Qt3DRender::QScreenRayCaster(m_rootEntity);
m_screenRayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::SingleShot);
m_rootEntity->addComponent(m_screenRayCaster);
/*
* Handle ray casting results by signal-slot connection
* "QScreenRayCaster::hitsChanged" signal contains ray casting result for any hit
* "MyClass::handleScreenRayCasterHits" slot needs to be implemented to handle hit results
*/
QObject::connect(m_screenRayCaster, &Qt3DRender::QScreenRayCaster::hitsChanged, this, &MyClass::handleScreenRayCasterHits);
我使用 m_screenRayCaster->trigger()
方法通过这样的触摸事件触发 QScreenRayCaster
测试:
void MyClass::HandleTouchEvent(QTouchEvent *event)
{
switch (event->type()) {
case QEvent::TouchBegin:
break;
case QEvent::TouchEnd:
if (event->touchPoints().count() == 1) {
QPointF point = event->touchPoints().at(0).pos();
m_screenRayCaster->trigger(QPoint(static_cast<int>(point.x()), static_cast<int>(point.y())));
}
break;
default:
break;
}
}
在 MyClass::handleScreenRayCasterHits
插槽中处理光线投射结果:
void MyClass::handleScreenRayCasterHits(const Qt3DRender::QAbstractRayCaster::Hits hits)
{
for (int i = 0; i < hits.length(); ++i) {
qDebug() << __func__ << "Hit Type: " << hits.at(i).type();
qDebug() << __func__ << "Hit entity name: " << hits.at(i).entity()->objectName();
}
}
如何使用 Qt3DRender::QObjectPicker 触摸事件?
我正在通过这种方法将 Qt3DRender::QObjectPicker
组件添加到我的 Qt3D 实体中:
Qt3DRender::QObjectPicker *MyClass::createObjectPickerForEntity(Qt3DCore::QEntity *entity)
{
Qt3DRender::QObjectPicker *picker = new Qt3DRender::QObjectPicker(entity);
picker->setHoverEnabled(false);
entity->addComponent(picker);
connect(picker, &Qt3DRender::QObjectPicker::pressed, this, &MyClass::handlePickerPress);
return picker;
}
我的对象选择器适用于鼠标点击,但不适用于触摸事件。有人知道如何在智能手机上使用 Qt3D 对象选择器和触摸事件吗?
@FlorianBlume 帮我解决了问题。可以使用 QScreenRayCaster
检测 Qt3D 实体上的触摸。我必须向我的根实体添加一个 QScreenRayCaster
组件:
/*
* You have to add the ray caster to the root entity as a component
* Perform ray casting tests by specifying "touch" coordinates in screen space
*/
m_screenRayCaster = new Qt3DRender::QScreenRayCaster(m_rootEntity);
m_screenRayCaster->setRunMode(Qt3DRender::QAbstractRayCaster::SingleShot);
m_rootEntity->addComponent(m_screenRayCaster);
/*
* Handle ray casting results by signal-slot connection
* "QScreenRayCaster::hitsChanged" signal contains ray casting result for any hit
* "MyClass::handleScreenRayCasterHits" slot needs to be implemented to handle hit results
*/
QObject::connect(m_screenRayCaster, &Qt3DRender::QScreenRayCaster::hitsChanged, this, &MyClass::handleScreenRayCasterHits);
我使用 m_screenRayCaster->trigger()
方法通过这样的触摸事件触发 QScreenRayCaster
测试:
void MyClass::HandleTouchEvent(QTouchEvent *event)
{
switch (event->type()) {
case QEvent::TouchBegin:
break;
case QEvent::TouchEnd:
if (event->touchPoints().count() == 1) {
QPointF point = event->touchPoints().at(0).pos();
m_screenRayCaster->trigger(QPoint(static_cast<int>(point.x()), static_cast<int>(point.y())));
}
break;
default:
break;
}
}
在 MyClass::handleScreenRayCasterHits
插槽中处理光线投射结果:
void MyClass::handleScreenRayCasterHits(const Qt3DRender::QAbstractRayCaster::Hits hits)
{
for (int i = 0; i < hits.length(); ++i) {
qDebug() << __func__ << "Hit Type: " << hits.at(i).type();
qDebug() << __func__ << "Hit entity name: " << hits.at(i).entity()->objectName();
}
}