Detecting touch on 3D objects, in addition to mouse click

女生的网名这么多〃 提交于 2019-12-23 17:07:39

问题


How to use Qt3DRender::QObjectPicker with touch events?

I'm adding Qt3DRender::QObjectPicker component to my Qt3D entities by this method:

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

My object picker works with mouse clicks, but it doesn't work with touch events. Does anybody know how I can use Qt3D object picker with touch events on smartphones?


回答1:


@FlorianBlume helped me to solve the problem. Touch on Qt3D entities can be detected with QScreenRayCaster. I had to add a QScreenRayCaster component to my root entity:

 /*
  * 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);

I trigger QScreenRayCaster tests by touch events like this using m_screenRayCaster->trigger() method:

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

}

Handling ray casting results in MyClass::handleScreenRayCasterHits slot:

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


来源:https://stackoverflow.com/questions/54074452/detecting-touch-on-3d-objects-in-addition-to-mouse-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!