MultiPointTouchArea behaviour in QQuickWidget

1.5k Views Asked by At

I'm trying to get the MultiPointTouchArea to work correctly inside a QQuickWidget. Consider the following example qml file(MultiPointTouchTest.qml):

import QtQuick 2.0

Rectangle {
    width: 360
    height: 480
    color: touch1.pressed ? "gray" : "black";

    MultiPointTouchArea {
        anchors.fill: parent
        minimumTouchPoints: 1
        maximumTouchPoints: 2
        enabled: true;
        touchPoints: [
            TouchPoint { id: touch1; objectName: "touch 1"; },
            TouchPoint { id: touch2; objectName: "touch 2"; }
        ]

        onGestureStarted: {
            gesture.grab();
        }
        onPressed: {
            console.log("---onPressed---");
            console.log(touch1.objectName, "pressed:", touch1.pressed, touch1.x, touch1.y);
            console.log(touch2.objectName, "pressed:", touch2.pressed, touch2.x, touch2.y);
        }
        onUpdated: {
            console.log("---onUpdated---");
            console.log(touch1.objectName, "pressed:", touch1.pressed, touch1.x, touch1.y);
            console.log(touch2.objectName, "pressed:", touch2.pressed, touch2.x, touch2.y);
        }
        onReleased: {
            console.log("---onReleased---");
            console.log(touch1.objectName, "pressed:", touch1.pressed, touch1.x, touch1.y);
            console.log(touch2.objectName, "pressed:", touch2.pressed, touch2.x, touch2.y);
        }
        onTouchUpdated: {
            console.log("---onTouchUpdated---");
            console.log(touch1.objectName, "pressed:", touch1.pressed, touch1.x, touch1.y);
            console.log(touch2.objectName, "pressed:", touch2.pressed, touch2.x, touch2.y);
        }
    onCanceled: {
            console.log("---onCanceled---");
            console.log(touch1.objectName, "pressed:", touch1.pressed, touch1.x, touch1.y);
        console.log(touch2.objectName, "pressed:", touch2.pressed, touch2.x, touch2.y);
        }
    }
}

In the main.cpp file, here I use QQuickWidget or QQuickView, like this:

QQuickWidget* quickWidget = new QQuickWidget(QUrl("qrc:///QML/qml/MultiPointTouchTest.qml"));
if (quickWidget->status() == QQuickWidget::Ready) {
    QQuickItem* quickItem = quickWidget->rootObject();
    quickItem->setProperty("width", QApplication::desktop()->width());
    quickItem->setProperty("height", QApplication::desktop()->height());
    quickWidget->resize(QApplication::desktop()->width(), QApplication::desktop()->height());
}

or

QQuickView* quickView = new QQuickView(QUrl("qrc:///QML/qml/MultiPointTouchTest.qml"));
... // like QQuickWidget's code

The MultiPointTouchArea's print is different info, for follow sequence operation:

  1. put my first finger to touch screen
  2. put my second finger to touch screen; the first finger touch becomes invalid, unimportant for now.
  3. remove my second finger; the console prints ---onCanceled---..., first finger touch is still invalid and touch1.pressed is false.

Using QQuickView this strange behaviour does not occur.

The Qt's document said for canceled signal, "This signal is emitted when new touch events have been canceled because another item stole the touch event handling."

I don't know document's mean after searched.

I try to read the source code to understand what the happen when I touch screen, but I fond QQuickWidget::event() send touch event(TouchBegin...) to QQuickWindow, and QQuickView::event = QQuickWindow, didn't reimplement.

So, what is real mean for the Qt's document?

I need QQuickWidget not QQuickView so, how can I use MultiPointTouchArea with QQuickWidget and have the correct expected behaviour?

2

There are 2 best solutions below

0
On

I had a similar issue and found that setting

quickWidget->setAttribute(Qt::WA_AcceptTouchEvents)

resolved it.

0
On

Main.cpp

QQuickWidget *content = new QQuickWidget(QUrl("qrc:/myPopup.qml"));
content->setAttribute(Qt::WA_TranslucentBackground);
content->setClearColor(Qt::transparent);
content->setAttribute(Qt::WA_AcceptTouchEvents);
scene.addWidget(content);

myPopup.qml

import QtQuick 2.13
import QtQuick.Controls 2.13

Rectangle {
  id:itemParent
  width: 90; height: 90
  color: "red"

  MultiPointTouchArea {
    anchors.fill: parent
    minimumTouchPoints: 1
    maximumTouchPoints: 2

    touchPoints: [
        TouchPoint { id: touch1; objectName: "touch 1"; }
    ]
    onPressed: {
        console.log(touch1.objectName, "pressed:", touch1.pressed, touch1.x, touch1.y);
    }
    onReleased: {
        console.log(touch1.objectName, "onReleased:", touch1.pressed, touch1.x, touch1.y);
    }
    onTouchUpdated: {
        console.log(touch1.objectName, "onTouchUpdated:", touch1.pressed, touch1.x, touch1.y);
    }
  }
}

even i've added content->setAttribute(Qt::WA_AcceptTouchEvents), it detects mouse instead of touch.