我目前正在为一个在后台使用openGL渲染的触摸设备实现一个QML应用程序。我使用这个talk https://www.youtube.com/watch?v=GYa5DLV6ADQ作为我工作的基础。
简而言之,为了能够绘制我的openGL内容,我使用了一个自定义的QQuickView,并将"clearbeforerendering“选项设置为false。除此之外,我希望在自定义QQuickView中接收touchEvents,以实时修改我的openGL呈现。
不幸的是,当我正确地接收不同的mouseEvents时,touchEvent永远不会被触发。我在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";
}有没有人知道为什么在自定义QQuickView中没有触发touchEvents?
发布于 2016-06-10 19:31:06
经过一些研究后,我发现touchEvents并没有在QQuickView中发送,尽管Qt文档中说了些什么。
为了解决这个问题,我必须在QWindow的Qt事件分派器中处理事件( QQuickView继承自QWindow::event (事件*))。我猜你可以自己传播事件或者在event函数中使用它,但是我想知道这是一个bug还是一个疏忽。
这是我用来结束它的代码,我不知道它是否干净,但它是有效的。
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);
}希望它可以帮助一些人,即使这个问题已经被否决了,没有给出任何理由。
https://stackoverflow.com/questions/37548494
复制相似问题