QML 警告上下文

问题描述 投票:0回答:1

以下是我的代码。

Window {
    id : mainWindow
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    flags: Qt.FramelessWindowHint  //无边框窗口(去掉title栏)

    MouseArea {
        id: mouseArea
        anchors.fill: parent

        property int mouse_x;
        property int mouse_y;

        onPressed: {
            mouse_x = mouse.x;
            mouse_y = mouse.y;
        }

        onMouseXChanged:{
            if(mouse.buttons === Qt.LeftButton)
            {
                mainWindow.x += mouse.x - mouse_x;
            }
        }

        onMouseYChanged: {
            if(mouse.buttons === Qt.LeftButton)
            {
                mainWindow.y += mouse.y - mouse_y;
            }
        }
    }
}

功能可以正常执行,但是控制台还是有一些警告文字

"Parameter "mouse" is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead."

有人可以教我如何解决这个问题并教我为什么会出现这个警告吗?

qt qml
1个回答
0
投票

来自MouseArea文档:

许多 MouseArea 信号传递一个 mouse 参数,该参数包含有关鼠标事件的附加信息,例如位置、按钮和任何键修饰符。

您必须在插槽中定义此mouse参数才能捕获它:

onMouseXChanged: (mouse) => {
    if(mouse.buttons === Qt.LeftButton)
    {
        mainWindow.x += mouse.x - mouse_x;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.