我想知道如何通过在Qt / QML中按住MouseArea来移动窗口。
答案 0 :(得分:7)
您可以使用以下内容将视图窗口小部件公开给QML:
QmlApplicationViewer viewer;
QDeclarativeContext *context = viewer.rootContext();
context->setContextProperty("viewerWidget", &viewer);
然后修改其pos
属性以移动窗口:
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)
}
}
}