先上演示效果,不过会有闪烁,暂时想到不到是什么问题。
去除标题栏
Window {
flags: Qt.Window | Qt.FramelessWindowHint //隐藏标题栏
}
点击标题栏移动窗口,我在窗口的最上方加了一个rectangle
Rectangle {
id:backRect;
anchors.fill: parent;
Rectangle {//用来移动的标题栏
id:moveTitle;
anchors.top: backRect.top;
anchors.left: backRect.left;
anchors.right: backRect.right;
height: 35;
MouseArea{ //设置可以拖动没有标题的登录界面. /*这个要放在上面,放在最下面的话,会把上面全部屏蔽掉的*/
anchors.fill:parent
property point clickPos: "0,0" //定义一个点
onPressed: {
clickPos = Qt.point(mouseX, mouseY)
}
onPositionChanged: { //属性的改变
var delta = Qt.point(mouseX-clickPos.x, mouseY-clickPos.y)
root.setX(root.x+delta.x)
root.setY(root.y+delta.y)
}
}
}
拖动改变大小实现了左下右,还有左下,右下,左上,右上,实现大概差不多,用不着,就不实现了。
需要注意的是,向左拖动的代码,因为整体窗口的坐标左上角是(0,0),
所以在拖动的时候会改变窗口的坐标。cursorShape可以改变鼠标的样子。
Rectangle {//向左拖动
id:leftSizeChange;
anchors.top: moveTitle.bottom;
anchors.bottom: downSizeChange.top;
anchors.left: backRect.left;
// height: root.height-35-downSizeChange.height;
width: 15;
color: "yellow"
MouseArea{ //设置可以拖动没有标题的登录界面. /*这个要放在上面,放在最下面的话,会把上面全部屏蔽掉的*/
anchors.fill:parent
property point clickPos: "0,0" //定义一个点
cursorShape:Qt.SizeHorCursor
onPressed: {
clickPos = Qt.point(mouseX, mouseY)
}
onPositionChanged: { //属性的改变
var delta = Qt.point(mouseX-clickPos.x, mouseY-clickPos.y)
if((root.width-delta.x)>root.minimumWidth)
{
root.setX(root.x+delta.x)
root.setWidth(root.width-delta.x)
}
else
root.setWidth(root.minimumWidth)
}
}
}
Rectangle {//向下拖动
id:downSizeChange;
anchors.bottom: backRect.bottom;
anchors.left: leftSizeChange.right;
anchors.right: rightSizeChange.left;
height: 10;//远离发送的按钮
color: "green"
MouseArea{ //设置可以拖动没有标题的登录界面. /*这个要放在上面,放在最下面的话,会把上面全部屏蔽掉的*/
anchors.fill:parent
property point clickPos: "0,0" //定义一个点
cursorShape:Qt.SizeVerCursor
onPressed: {
clickPos = Qt.point(mouseX, mouseY)
}
onPositionChanged: { //属性的改变
var delta = Qt.point(mouseX-clickPos.x, mouseY-clickPos.y)
if((root.height+delta.y)>root.minimumHeight)
// root.setHeight(root.height+delta.y)
root.height += delta.y
else
root.setHeight(root.minimumHeight)
}
}
}
Rectangle {//向右拖动
id:rightSizeChange;
width: 15;
anchors.right: backRect.right;
anchors.top:moveTitle.bottom;
anchors.bottom: downSizeChange.top;
color: "yellow";
MouseArea{ //设置可以拖动没有标题的登录界面. /*这个要放在上面,放在最下面的话,会把上面全部屏蔽掉的*/
anchors.fill:parent
property point clickPos: "0,0" //定义一个点
cursorShape:Qt.SizeHorCursor
onPressed: {
clickPos = Qt.point(mouseX, mouseY)
}
onPositionChanged: { //属性的改变
var delta = Qt.point(mouseX-clickPos.x, mouseY-clickPos.y)
if((root.width+delta.x)>root.minimumWidth)
{
// root.setX(root.x+delta.x)
root.setWidth(root.width+delta.x)
}
else
root.setWidth(root.minimumWidth)
}
}
}