QML警告上下文

huangapple go评论55阅读模式
英文:

QML warning context

问题

以下是您的代码翻译:

窗口 {
    id : mainWindow
    宽度: 640
    高度: 480
    可见: true
    标题: qsTr("Hello World")

    标志: Qt.FramelessWindowHint  // 无边框窗口(去掉标题栏)

    鼠标区域 {
        id: mouseArea
        锚点.fill: 父项

        属性 int 鼠标_x;
        属性 int 鼠标_y;

        onPress: {
            鼠标_x = 鼠标.x;
            鼠标_y = 鼠标.y;
        }

        onMouseXChanged:{
            if(鼠标.buttons === Qt.LeftButton)
            {
                mainWindow.x += 鼠标.x - 鼠标_x;
            }
        }

        onMouseYChanged: {
            if(鼠标.buttons === Qt.LeftButton)
            {
                mainWindow.y += 鼠标.y - 鼠标_y;
            }
        }
    }
}

至于您提到的警告问题,警告信息是说参数"mouse"没有声明。这是因为在您的代码中,您在信号处理程序中使用了"mouse"变量,但未显式声明它。这种注入参数到信号处理程序的方式在Qt中已被弃用,现在建议使用带有形式参数的JavaScript函数来代替。要解决这个警告,您可以将鼠标事件的处理程序重写为JavaScript函数,并在函数中声明"mouse"作为参数。这样警告就不会再出现了。

英文:

The following is my code.

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

The function can be mplemented normally but there is still some warning text in the console.

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

Could someone teach me how to solve this problem and teach me why this warning would happened?

答案1

得分: 1

MouseArea文档中:

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

您需要在您的槽中定义这个mouse参数以捕获它:

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

From the MouseArea documentation:

Many MouseArea signals pass a mouse parameter that contains additional information about the mouse event, such as the position, button, and any key modifiers.

You have to define this mouse parameter in your slot to capture it:

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

huangapple
  • 本文由 发表于 2023年5月11日 14:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定