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

 

转载于:https://www.cnblogs.com/yanhuiw/p/4008138.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值