gpt4 book ai didi

qt3d - 除了鼠标单击之外,还可以检测 3D 对象上的触摸

转载 作者:行者123 更新时间:2023-12-03 23:48:19 24 4
gpt4 key购买 nike

使用方法 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);


我触发 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投币口:

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

关于qt3d - 除了鼠标单击之外,还可以检测 3D 对象上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54074452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com