gpt4 book ai didi

c++ - 未在 QQuickView 上触发触摸事件

转载 作者:行者123 更新时间:2023-11-28 02:05:53 25 4
gpt4 key购买 nike

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

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

不幸的是,touchEvent 从未被触发,而我正确地收到了不同的 mouseEvents。我在 QOPenGLWindow 上试过这段代码并正确接收了 touchEvents,所以问题不是出在我的设备上。

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

主要.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();
}

主.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";
}

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

最佳答案

经过一些研究,我发现 touchEvents 没有在 QQuickView 中调度,尽管 Qt 文档中有说明。

为了解决这个问题,我必须在 QWindow(QQuickView 继承自)QWindow::event(Event*) 的 Qt 事件调度程序中处理 touchEvents。我想您可以自己传播事件或按原样在事件函数中使用它,但我想知道这是错误还是疏忽。

这是我最终得到的代码,我不知道它是否干净但它可以工作..

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

希望即使这个问题在没有给出理由的情况下被否决,它也可能对某人有所帮助。

关于c++ - 未在 QQuickView 上触发触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37548494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com