QtQuick2 dragging frameless window

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

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

Main different in the code is that my code uses QtQuick2ApplicationViewer instead of QmlApplicationViewer and it looks like QtQuick2ApplicationViewer do not have “.pos” property.

This is my main.cpp

#include  #include "qtquick2applicationviewer.h" #include   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 window I get this error:

TypeError: Cannot read property 'x' of undefined

Any ideas ? Is it even possible with QtQuick2 ? Thanks for help!

回答1:

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.



回答2:

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 


回答3:

I done it as follow:

Window {     id: window     height: 400     width: 250     x: (Screen.width - width)/2     //

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!