0

我试图让 MultiPointTouchArea 在 QQuickWidget 中正常工作。考虑以下示例 qml 文件 (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);
        }
    }
}

在 main.cpp 文件中,这里我使用 QQuickWidget 或 QQuickView,如下所示:

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

或者

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

MultiPointTouchArea 的打印是不同的信息,用于以下顺序操作:

  1. 把我的第一根手指放在触摸屏上
  2. 将我的第二根手指放在触摸屏上;第一次手指触摸无效,暂时不重要。
  3. 移开我的第二根手指;控制台打印 ---onCanceled---...,第一根手指触摸仍然无效,并且 touch1.pressed 为假。

使用 QQuickView 不会发生这种奇怪的行为。

Qt 的文档说取消信号,“当新的触摸事件被取消时发出这个信号,因为另一个项目偷走了触摸事件处理。”

搜索后我不知道文档的意思。

我尝试阅读源代码以了解触摸屏时会发生什么,但我喜欢 QQuickWidget::event() 将触摸事件(TouchBegin...)发送到 QQuickWindow,并且 QQuickView::event = QQuickWindow,没有重新实现.

那么,Qt 文档的真正含义是什么?

我需要 QQuickWidget 而不是 QQuickView 所以,如何将 MultiPointTouchArea 与 QQuickWidget 一起使用并获得正确的预期行为?

4

2 回答 2

1

我有一个类似的问题,发现设置

quickWidget->setAttribute(Qt::WA_AcceptTouchEvents)

解决了它。

于 2015-07-20T12:13:27.180 回答
0

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.

于 2020-03-16T11:06:36.317 回答