qml 去除标题栏后 拖动窗口和改变窗口大小

9 篇文章 0 订阅

先上演示效果,不过会有闪烁,暂时想到不到是什么问题。
在这里插入图片描述

去除标题栏

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)
                }
            }
        }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值