如何在QML无窗口应用程序中设置拖放区域

How to set the drag area in qml windowless application

本文关键字:设置 拖放 区域 应用程序 窗口 QML      更新时间:2023-10-16

我的问题是我如何设置在没有窗口的情况下拖动应用程序我看到了许多可以在台式机上拖动鼠标的应用程序。我的应用使用QML,因此任何可能的方法都可以获得这项工作。

取您的MouseArea::positionChanged信号并使用位置delta(您必须在每个调用上保存最后一个位置,以便您可以计算Delta)以更新您的Window::xy属性。

Window {
    id: win
    width: 200
    height: 200
    MouseArea {
        anchors.fill: parent
        property int lastX
        property int lastY
        onPositionChanged: {
            // Remap the mouse coords back to the window.  Not
            // necessary in this example, but will be in 'real'
            // use.
            var mPos = mapToItem( null, mouse.x, mouse.y );
            mPos.x += win.x;
            mPos.y += win.y;
            // Skip the first iteration by testing if the properties
            // are defined, otherwise the window will jump.
            if ( lastX && lastY ) {
                win.x += mPos.x - lastX;
                win.y += mPos.y - lastY;
            }
            lastX = mPos.x;
            lastY = mPos.y;
        }
    }
}