除了单击鼠标外,还检测3D对象的触摸

时间:2019-01-07 12:27:10

标签: qt3d

如何在触摸事件中使用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对象选择器吗?

1 个答案:

答案 0 :(得分:2)

@FlorianBlume帮助我解决了这个问题。可以使用Class<Post> entityClass = Post.class; CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> query = cb.createTupleQuery(); Root<?> root = query.from(entityClass); query.select( cb.tuple( root.get("id"), cb.function("now", Date.class) ) ); 检测到Qt3D实体上的触摸。我必须在我的根实体中添加一个QScreenRayCaster组件:

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); 方法通过类似的触摸事件触发QScreenRayCaster测试:

m_screenRayCaster->trigger()

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