Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving the window on holding Qml MouseArea

Tags:

qt

qml

I want to know how to move the window by press and holding the MouseArea in Qt/QML.

like image 797
herophuong Avatar asked Apr 18 '12 05:04

herophuong


1 Answers

You can expose the view widget to QML with:

QmlApplicationViewer viewer;
QDeclarativeContext *context = viewer.rootContext();
context->setContextProperty("viewerWidget", &viewer);

Then modify its pos property to move the window:

MouseArea {        
    anchors.fill: parent
    property variant previousPosition        
    onPressed: {
        previousPosition = Qt.point(mouseX, mouseY)
    }
    onPositionChanged: {
        if (pressedButtons == Qt.LeftButton) {
            var dx = mouseX - previousPosition.x
            var dy = mouseY - previousPosition.y
            viewerWidget.pos = Qt.point(viewerWidget.pos.x + dx, 
                                        viewerWidget.pos.y + dy)
        }
    }
}    
like image 119
alexisdm Avatar answered Nov 09 '22 22:11

alexisdm