码迷,mamicode.com
首页 > 其他好文 > 详细

osgQt支持触摸屏

时间:2014-10-06 15:26:50      阅读:484      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

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() << "\t" << "y:" << touchPos.y() << "\t";

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

 

osgQt支持触摸屏

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/yanhuiw/p/4008138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!