QtQuick2 dragging frameless window

后端 未结 4 1975
北恋
北恋 2020-12-10 06:17

I’m looking for a way of dragging frameless window in QtQuick2. I followed this thread on the forum Link but it gives me an error.

Main difference in the code is tha

相关标签:
4条回答
  • 2020-12-10 06:39

    In my project I do:

    property variant clickPos: "1,1"
    
    onPressed: {
        clickPos  = Qt.point(mouse.x,mouse.y)
    }
    
    onPositionChanged: {
        var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
        rootWindow.x += delta.x;
        rootWindow.y += delta.y;
    }
    

    In MouseArea.

    0 讨论(0)
  • 2020-12-10 06:40

    Also to resemble Windows behaviour of maximizing window when dragging it above vertical edge of the screen:

    MouseArea {
        anchors.fill: parent;
        property variant clickPos: "1,1"
    
        onPressed: {
            clickPos = Qt.point(mouse.x,mouse.y)
        }
    
        onPositionChanged: {
            var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
            var new_x = mainWindow.x + delta.x
            var new_y = mainWindow.y + delta.y
            if (new_y <= 0)
                mainWindow.visibility = Window.Maximized
            else
            {
                if (mainWindow.visibility === Window.Maximized)
                    mainWindow.visibility = Window.Windowed
                mainWindow.x = new_x
                mainWindow.y = new_y
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-10 06:47

    A rather complete example:

    import QtQuick 2.14
    import QtQuick.Controls 2.14
    
    ApplicationWindow {
        visible: true
        width: 200
        height: 200
        flags: Qt.FramelessWindowHint
        MouseArea {
            anchors.fill: parent
            onPressed: { pos = Qt.point(mouse.x, mouse.y) }
            onPositionChanged: {
                var diff = Qt.point(mouse.x - pos.x, mouse.y - pos.y)
                ApplicationWindow.window.x += diff.x
                ApplicationWindow.window.y += diff.y
            }
            property point pos
        }
    }
    
    0 讨论(0)
  • 2020-12-10 06:56

    I done it as follow:

    Window {
        id: window
        height: 400
        width: 250
        x: (Screen.width - width)/2     //<---start position of window
        y: (Screen.height - height)/2   //<-┘
        color: "transparent"
        flags: Qt.MSWindowsFixedSizeDialogHint | Qt.FramelessWindowHint
    
        MouseArea {
            anchors.fill: parent
            property point lastMousePos: Qt.point(0, 0)
            onPressed: { lastMousePos = Qt.point(mouseX, mouseY); }
            onMouseXChanged: window.x += (mouseX - lastMousePos.x)
            onMouseYChanged: window.y += (mouseY - lastMousePos.y)
        }
    
        Item {
            id: myFirstPage
            anchors.fill: parent
            anchors.topMargin: 50
            //...
            //This item can have some mouse area
        }
    }
    

    Now you can move window using dragging top 50 pixels or any where that is not under any MouseArea.

    0 讨论(0)
提交回复
热议问题