我正试图让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.pressed为false
使用QQuickView不会出现这种奇怪的行为。
Qt的文件称,对于取消的信号,"当新的触摸事件因另一个项目窃取了触摸事件处理而被取消时,就会发出该信号。"
经过搜索,我不知道这份文件的意思。
我试着阅读源代码,以了解当我触摸屏幕时会发生什么,但我喜欢QQuickWidget::event()将触摸事件(TouchBegin…)发送到QQuickWindow,而QQuickView::event=QQuickWindow没有重新实现。
那么,Qt文件的真正含义是什么呢?
我需要QQuickWidget而不是QQuickView,所以,我如何将MultiPointTouchArea与QQuickWidget一起使用,并具有正确的预期行为?
我遇到了类似的问题,发现设置
quickWidget->setAttribute(Qt::WA_AcceptTouchEvents)
解决了它。
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);
}
}
}
即使我添加了content->setAttribute(Qt::WA_AcceptTouchEvents),它也检测鼠标而不是触摸。