未在 QQuickView 上触发触摸事件

Hug*_*rne 2 c++ opengl qt qml

我目前正在为在后台使用 openGL 渲染的触摸设备实现 QML 应用程序。我使用这个演讲https://www.youtube.com/watch?v=GYa5DLV6ADQ作为我工作的基础。

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

不幸的是,touchEvent 永远不会被触发,而我正确地收到了不同的 mouseEvents。我在 QOPenGLWindow 上尝试了此代码并正确接收了 touchEvents,因此问题不是来自我的设备。

以下是我的代码的一部分,可能会有所帮助:

主程序

#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();
}
Run Code Online (Sandbox Code Playgroud)

主文件

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"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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";
 }
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么在自定义 QQuickView 中没有触发 touchEvents ?

Hug*_*rne 5

经过一些研究,我发现尽管 Qt 文档中说了些什么,但在 QQuickView 中并未调度 touchEvents。

为了解决这个问题,我必须在 QWindow 的 Qt 事件调度器中处理 touchEvents(从 QQuickView 继承)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);
     }
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助某人,即使该问题在没有给出任何理由的情况下被否决。