osgQt支持触摸屏

1. osgQt的构造函数添加:setAttribute(Qt::WA_AcceptTouchEvents);//wyh

2. event()修改,支持触摸时间

bool GLWidget::event( QEvent* event )
{

    // QEvent::Hide
    //
    // workaround "Qt-workaround" that does glFinish before hiding the widget
    // (the Qt workaround was seen at least in Qt 4.6.3 and 4.7.0)
    //
    // Qt makes the context current, performs glFinish, and releases the context.
    // This makes the problem in OSG multithreaded environment as the context
    // is active in another thread, thus it can not be made current for the purpose
    // of glFinish in this thread.

    // QEvent::ParentChange
    //
    // Reparenting GLWidget may create a new underlying window and a new GL context.
    // Qt will then call doneCurrent on the GL context about to be deleted. The thread
    // where old GL context was current has no longer current context to render to and
    // we cannot make new GL context current in this thread.

    // We workaround above problems by deferring execution of problematic event requests.
    // These events has to be enqueue and executed later in a main GUI thread (GUI operations
    // outside the main thread are not allowed) just before makeCurrent is called from the
    // right thread. The good place for doing that is right after swap in a swapBuffersImplementation.

    if (event->type() == QEvent::Hide)
    {
        // enqueue only the last of QEvent::Hide and QEvent::Show
        enqueueDeferredEvent(QEvent::Hide, QEvent::Show);
        return true;
    }
    else if (event->type() == QEvent::Show)
    {
        // enqueue only the last of QEvent::Show or QEvent::Hide
        enqueueDeferredEvent(QEvent::Show, QEvent::Hide);
        return true;
    }
    else if (event->type() == QEvent::ParentChange)
    {
        // enqueue only the last QEvent::ParentChange
        enqueueDeferredEvent(QEvent::ParentChange);
        return true;
    }
    //wyh
    else if(event->type() == QEvent::TouchBegin ||event->type() == QEvent::TouchUpdate || event->type() == QEvent::TouchEnd)
    {
        QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
        if(touchPoints.count() >= 2)
        {
            //test
            //std::cout << "touch multiViewer" << std::endl;    
            //std::cout << std::endl;

            osg::ref_ptr<osgGA::GUIEventAdapter> osg_event(NULL);
            foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints)
            {
                QPointF touchPos = touchPoint.pos();

                ////test
                //std::cout << "x:" << touchPos.x() << "	" << "y:" << touchPos.y() << "	";

                if(touchPoint.state() == Qt::TouchPointPressed)
                {
                    if (!osg_event) {
                        osg_event =  _gw->getEventQueue()->touchBegan( touchPoint.id(), osgGA::GUIEventAdapter::TOUCH_BEGAN, touchPos.x()  , touchPos.y());
                    } else {
                        osg_event->addTouchPoint( touchPoint.id(), osgGA::GUIEventAdapter::TOUCH_BEGAN, touchPos.x() , touchPos.y());
                    }
                }
                else if(touchPoint.state() == Qt::TouchPointMoved)
                {
                    if (!osg_event) {
                        osg_event =  _gw->getEventQueue()->touchMoved( touchPoint.id(), osgGA::GUIEventAdapter::TOUCH_MOVED, touchPos.x(), touchPos.y());
                    } else {
                        osg_event->addTouchPoint( touchPoint.id(), osgGA::GUIEventAdapter::TOUCH_MOVED, touchPos.x() , touchPos.y());
                    }
                }
                else if(touchPoint.state() == Qt::TouchPointReleased)
                {
                    // No double tap detection with RAW TOUCH Events, sorry.
                    if (!osg_event) {
                        osg_event =  _gw->getEventQueue()->touchEnded( touchPoint.id(), osgGA::GUIEventAdapter::TOUCH_ENDED, touchPos.x(), touchPos.y(), 1);
                    } else {
                        osg_event->addTouchPoint( touchPoint.id(), osgGA::GUIEventAdapter::TOUCH_ENDED, touchPos.x() , touchPos.y());
                    }
                }
            }
            return true;
        }

    }

    // perform regular event handling
    return QGLWidget::event( event );
}
原文地址:https://www.cnblogs.com/yanhuiw/p/4008138.html