【问题标题】:How to recieve Qt signal entered() in MouseArea from Touch?如何从 Touch 接收 MouseArea 中输入的 Qt 信号?
【发布时间】:2016-09-14 10:36:43
【问题描述】:

我在 QT 应用程序中将 MouseArea 用于按钮,并且需要知道用户何时在触摸按钮时移动手指。在 W7 上使用鼠标,我从 MouseArea 使用 onEntered 获取此信息。

问题:在带有触摸屏的目标平台上,只有当我按下 MouseArea(首先触摸 MouseArea 内),然后将手指移出(exited-signal),然后再将手指移回时,才会触发输入信号(输入信号)。但是当第一次触摸在该区域之外并且我进入它时,不会触发输入信号。 我还尝试了使用 onTouchUpdated 和 onUpdated 的 MultiPointTouchArea,同样的行为。

我在嵌入式 linux 上使用 Qt 5.5.1。

我可以做些什么来让 MouseArea 总是触发输入的信号吗?还是应该使用 MouseArea 或 MultiPointTouchArea 以外的东西?

作为一种解决方法,我可以在整个窗口上考虑一个 MouseArea,其中包含所有按钮所在的信息,并检查每个 positionChanged(如果输入了按钮),但我确信一定有更好的解决方案。

编辑:在目标平台上使用鼠标时,将鼠标移入该区域时会触发输入信号。

这是我的(简化的)代码:

main.cpp

#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtQml>

#include "uart_terminal.h"

int main(int argc, char *argv[])
{
    system("mount /dev/mmcblk0p1 /mnt/sdcard/");
    setenv("QT_QPA_EGLFS_PHYSICAL_WIDTH", "376", 1);
    setenv("QT_QPA_EGLFS_PHYSICAL_HEIGHT", "301", 1);

    QApplication app(argc, argv);
    qmlRegisterType<UART_terminal>("com.uart", 1, 0, "UARTTerminal");

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("applicationDirPath", QGuiApplication::applicationDirPath());
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

main.qml:

    Window
    {
        id: rootWindow
        visible: true

        width: 1280
        height: 1024

        Rectangle
        {
            id: test
            color: "red"
            width: 300
            height: 300

            MouseArea
            {
                anchors.fill: parent

                hoverEnabled: true
                acceptedButtons: Qt.AllButtons

                onEntered:
                {
                    console.log("entered")
                    test.color = "blue"
                }
                onExited: test.color = "red"
                onCanceled: console.log("canceled")
                onPositionChanged:
                {
                    console.log("pos changed, containsMouse", containsMouse)
                }
            }

            /*MultiPointTouchArea
            {
                anchors.fill: parent
                onTouchUpdated: console.log("touch updated")
                onUpdated: console.log("touchlist updated")
            }*/
        }
    }

【问题讨论】:

    标签: qt touch qml


    【解决方案1】:

    因此,由于我没有找到任何其他解决方案,因此我不得不实施上述工作。我在整个屏幕上放置了一个 MouseArea “buttonContainer”,位于所有其他 MouseAreas 的后面。请注意,我必须从每个 MouseArea 传播 positionChanged 信号,否则当我开始触摸一个按钮然后输入另一个按钮时,它不会创建我的按钮输入信号。

    onPositionChanged:
    {
        // propagate event to buttonContainer with absolute mouse position
        var position = roundMouseArea.mapToItem(root)
        buttonContainer.xOffsetMouse = position.x
        buttonContainer.yOffsetMouse = position.y
        buttonContainer.positionChanged(mouse)
    }
    

    然后在 buttonContainer 的 onPositionChanged 中,我必须检查所有当前可见的按钮(如果已输入)。不要忘记使用 btn.mapToItem(buttonContainer) 再次映射每个按钮的位置,并在 buttonContainer 的 onPressed 中重置 x- 和 yOffsetMouse。

    就我而言,我必须做更多的事情,比如

    • 检查按钮是否被禁用,尽管可见
    • 记得输入Btn
    • 如果每个按钮已经从我的容器中输入,则为每个按钮设置 bool,因此当我停留在该按钮中时,我不会在每个 positionChanged 处收到输入信号
    • 还检查按钮是否退出
    • 相应地设置 btnPressed 真或假,例如。也在 buttonContainer 的 onReleased
    • 在引用enteredBtn 的任何方法或属性(例如,在buttonContainer 的onReleased 处)之前,相应地将enteredBtn 重置为未定义并检查它

    我认为仅此而已。不想在这里复制我的整个代码,只提一些如果您遇到相同问题可能还需要考虑的方面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      相关资源
      最近更新 更多