2016-05-31 109 views
0

我目前正在实现一个在后台使用openGL渲染的触摸设备的QML应用程序。我用这个谈话https://www.youtube.com/watch?v=GYa5DLV6ADQ作为我工作的基础。在QQuickView上没有触发的触摸事件

总之,我正在使用自定义QQuickView并将选项“clearbeforerendering”设置为false以便能够绘制我的openGL内容。除此之外,我想在我的自定义QQuickView中接收touchEvents以实时修改我的OpenGL渲染。

Unfornately,touchEvent从不触发,而我正确接收不同的mouseEvents。我在QOPenGLWindow上试过这段代码并正确接收了touchEvents,所以问题不是来自我的设备。

这里是我的代码中的一些部分可能会有所帮助:

的main.cpp

#include "tableclothwindow.h" 
#include "target.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    QDesktopWidget dw; 
    TableclothWindow *mainWindow = new TableclothWindow(); 

    mainWindow->setMinimumSize(QSize(dw.width(),dw.height())); 
    mainWindow->setSource(QUrl(QStringLiteral("qrc:/main.qml"))); 
    mainWindow->show(); 
    mainWindow->initializeQMLInteraction(); 

    return app.exec(); 
} 

main.qml

Item 
{ 
    id: container 
    Text { 
     objectName: "text" 
     id: helloText 
     text: "Hello world!" 
     x: 100 
     y: 80 
     color: "#00FF00" 
     opacity: 0.8 
     font.pointSize: 24; font.bold: true 
     MouseArea{ 
      onClicked: helloText.color = "FF0000" 
     } 
    } 
} 

tablecloth.cpp(定制QQuickView)

#include "tableclothwindow.h" 
#include "tableclothscene.h" 

TableclothWindow::TableclothWindow(QWindow *parent) 
    :QQuickView(parent), 
     m_mousePressed(false), 
     m_firstTime(true), 
     m_targetCentroid(QPointF(0,0)), 
     m_scene(new TableclothScene) 
{ 
    // disable auto-clear for manual openGL rendering 
    setClearBeforeRendering(false); 
    QObject::connect(this, SIGNAL(beforeRendering()), SLOT(renderOpenGLScene()), Qt::DirectConnection); 

    // update visuals 
    m_timer = new QTimer(this); 
    connect(m_timer, SIGNAL(timeout()), this, SLOT(update())); 
    m_timer->start(30); 
} 

TableclothWindow::~TableclothWindow() 
{ 

} 

// openGL rendering functions 
void TableclothWindow::renderOpenGLScene() 
{ 
    if(m_firstTime){ 
     m_scene->initialize(); 
     m_scene->resize(width(),height()); 
     assignLinkedPointMass(); 
     m_firstTime = false; 
    } 
    m_scene->render(); 
} 

void TableclothWindow::update() 
{ 
    if(!m_firstTime){ 
     updateTargetPosition(); 
     m_scene->update(); 
     QQuickView::update(); 
    } 
} 

// event handling 
// working 
void TableclothWindow::mousePressEvent(QMouseEvent *event) 
{ 
    event->accept(); 
    qDebug() << "mouse pressed" 

} 

// Doesn't work 
void TableclothWindow::touchEvent(QTouchEvent *event) 
{ 
    event->accept(); 
    qDebug() << " touch detected"; 
} 

做一个nyone知道为什么touchEvents不会在自定义QQuickView中被触发?

回答

1

经过一番研究,我发现touchEvents并没有在QQuickView中发送,尽管Qt文档中提到了什么。

为了解决这个问题,我不得不处理QWindow的Qt事件调度程序中的touchEvent(QuuView继承自QWindow :: event(Event *))。我想你可以自己传播事件或者像事件函数中那样使用它,但是我想知道它是一个错误还是一个疏忽。

这里是我结束它了,我不知道这是否是干净的,但它的工作原理代码..

bool TableclothWindow::event(QEvent *event) 
    { 
     event->accept(); 
     if(event->type() == QEvent::TouchEvent){ 
      QTouchEvent *touchEvent = static_cast<QTouchEvent*>(event); 
      //handling the touchEvent 
     } 
     return QQuickView::event(event); 
    } 

希望它可以帮助别人,即使问题已经下来了,没有投票给出的理由。