【Qt Quick】去除边框后自己实现拉伸

效果展示

在这里插入图片描述

代码部分

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)
                }
            }
        }

}


减少拖拽时窗口的闪烁

链接

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非西昂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值