QQuickWidget中的MultiPointTouchArea行为

时间:2015-04-05 08:48:13

标签: qt qml multipoint qquickview qquickwidget

我正在尝试让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为false。
  4. 使用QQuickView不会发生这种奇怪的行为。

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

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

    我尝试阅读源代码以了解触摸屏时发生的情况, 但我喜欢QQuickWidget :: event()发送触摸事件(TouchBegin ...)到QQuickWindow,而QQuickView :: event = QQuickWindow,没有重新实现。

    那么,Qt的文件真正意味着什么呢?

    我需要QQuickWidget而不是QQuickView所以,我如何在QQuickWidget中使用MultiPointTouchArea并具有正确的预期行为?

2 个答案:

答案 0 :(得分:1)

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

public class TestModel
{
    private readonly Repository _repository = ObjectFactory.GetRepositoryInstance();

    ...

    public int SelectedCategory { get; set; }
    public IEnumerable<CategoryModel> Categories {
        get
        {
            return _repository.Categories.Select(c => new CategoryModel
            {
                Id = c.Id,
                Name = c.Name
            });
        }
    }

    ...
}

解决了它。

答案 1 :(得分: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);
    }
  }
}

即使我添加了content-> setAttribute(Qt :: WA_AcceptTouchEvents),它也会检测鼠标而不是触摸。