效果展示
代码部分
将
target
值改为主窗口id。
import QtQuick 2.0
/*窗口缩放功能(未使用)*/
Item {
id:cws_ROOT
anchors.fill: parent
property var target: main_Window //目标窗口
property var tempLocationX:0
property var tempLocationY:0
property point startPoint: Qt.point(0,0)//记录鼠标的起点
//左
MouseArea{
id:left_MA
width: 5
height: parent.height - 10
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeHorCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetX = mouseX - startPoint.x
//跟随鼠标的移动改变窗口的宽度
if((target.width - offsetX) >= target.minimumWidth){
target.x += offsetX
target.width -= offsetX
}else{//限制窗口最小宽度
target.x += (target.width - target.minimumWidth)
target.width -= (target.width - target.minimumWidth)
}
}
}
//右
MouseArea{
id:right_MA
width: 5
height: parent.height - 10
anchors.verticalCenter: parent.verticalCenter
anchors.right: parent.right
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeHorCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetX = mouseX - startPoint.x
//跟随鼠标的移动改变窗口的宽度
if((target.width + offsetX) >= target.minimumWidth){
target.width += offsetX
}else{
target.width -= (target.width - target.minimumWidth)
}
}
}
//上
MouseArea{
id:top_MA
width: parent.width - 10
height: 5
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeVerCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetY = mouseY - startPoint.y
//跟随鼠标的移动改变窗口的宽度
if((target.height - offsetY) >= target.minimumHeight){
target.height -= offsetY
target.y += offsetY
}else{
target.y += (target.height - target.minimumHeight)
target.height -= (target.height - target.minimumHeight)
}
}
}
//下
MouseArea{
id:bottom_MA
width: parent.width - 10
height: 5
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeVerCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetY = mouseY - startPoint.y
//跟随鼠标的移动改变窗口的宽度
if((target.height + offsetY) >= target.minimumHeight){
target.height += offsetY
}else{
target.height -= (target.height - target.minimumHeight)
}
}
}
//左上
MouseArea{
id:leftTop_MA
width: 5
height: 5
anchors.left: parent.left
anchors.top: parent.top
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeFDiagCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetX = mouseX - startPoint.x
var offsetY = mouseY - startPoint.y
if((target.height - offsetY) >= target.minimumHeight){
target.height -= offsetY
target.y += offsetY
}else{
target.height -= (target.height - target.minimumHeight)
}
if((target.width - offsetX) >= target.minimumWidth){
target.x += offsetX
target.width -= offsetX
}else{//限制窗口最小宽度
target.x += (target.width - target.minimumWidth)
target.width -= (target.width - target.minimumWidth)
}
}
}
//右上
MouseArea{
id:rightTop_MA
width: 5
height: 5
anchors.right: parent.right
anchors.top: parent.top
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeBDiagCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetX = mouseX - startPoint.x
var offsetY = mouseY - startPoint.y
if((target.height - offsetY) >= target.minimumHeight){
target.height -= offsetY
target.y += offsetY
}else{
target.height -= (target.height - target.minimumHeight)
}
if((target.width + offsetX) >= target.minimumWidth){
target.width += offsetX
}else{
target.width -= (target.width - target.minimumWidth)
}
}
}
//左下
MouseArea{
id:leftBottom_MA
width: 5
height: 5
anchors.left: parent.left
anchors.bottom: parent.bottom
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeBDiagCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetX = mouseX - startPoint.x
var offsetY = mouseY - startPoint.y
if((target.width - offsetX) >= target.minimumWidth){
target.x += offsetX
target.width -= offsetX
}else{//限制窗口最小宽度
target.x += (target.width - target.minimumWidth)
target.width -= (target.width - target.minimumWidth)
}
if((target.height + offsetY) >= target.minimumHeight){
target.height += offsetY
}else{
target.height -= (target.height - target.minimumHeight)
}
}
}
//右下
MouseArea{
id:rightBottom_MA
width: 5
height: 5
anchors.right: parent.right
anchors.bottom: parent.bottom
acceptedButtons: Qt.LeftButton//只检测鼠标左键
propagateComposedEvents: true
cursorShape: Qt.SizeFDiagCursor
onPressed: {startPoint = Qt.point(mouseX,mouseY);}
onPositionChanged: {
var offsetX = mouseX - startPoint.x
var offsetY = mouseY - startPoint.y
if((target.width + offsetX) >= target.minimumWidth){
target.width += offsetX
}else{
target.width -= (target.width - target.minimumWidth)
}
if((target.height + offsetY) >= target.minimumHeight){
target.height += offsetY
}else{
target.height -= (target.height - target.minimumHeight)
}
}
}
}