Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use Qt3DRender::QObjectPicker with touch events on smartphones?
Forum Updated to NodeBB v4.3 + New Features

How to use Qt3DRender::QObjectPicker with touch events on smartphones?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 426 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m3g1dd
    wrote on last edited by m3g1dd
    #1

    How to use Qt3DRender::QObjectPicker with touch event?

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

    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 Reply Last reply
    0
    • M Offline
      M Offline
      m3g1dd
      wrote on last edited by m3g1dd
      #2

      It can be done with 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, e.g. by touch events
        */
          QObject::connect(m_screenRayCaster, &Qt3DRender::QScreenRayCaster::hitsChanged, this, &MyClass::handleScreenRayCasterHits);
      
      

      Triggering QScreenRayCaster by touch events:

      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 a 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();
          }
      }
      
      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved