QtQuick2 drag frameless window

I need a way to drag a boundless window into QtQuick2. I applied this thread on the Link forum , but it gave me an error.

The main difference in the code is that my code uses QtQuick2ApplicationViewer instead of QmlApplicationViewer, and it looks like QtQuick2ApplicationViewer does not have the ".pos" property.

This is my main.cpp

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include <QQmlContext>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("QmlApplicationViewer", (QObject *)&viewer);
    viewer.setFlags(Qt::FramelessWindowHint);
    viewer.setMainQmlFile(QStringLiteral("qml/ubusell/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

This is part of my main.qml

MouseArea {
    id: mouseRegion
    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)
            print(QmlApplicationViewer.pos)
            QmlApplicationViewer.pos = (20,20)
            QmlApplicationViewer.pos = Qt.point(QmlApplicationViewer.pos.x+delta.x,
                              QmlApplicationViewer.pos.y+delta.y)
        }
}

When I try to drag a window, I get this error:

TypeError: cannot read property 'x' undefined

Any ideas? Is this possible with QtQuick2? Thanks for the help!

+4
source share
3 answers

In my project, I:

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.

+9

, Windows :

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
        }
    }
}
+1

I did it as follows:

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 the window by dragging and dropping 50 pixels or any other where no MouseArea is located.

0
source

All Articles