Qt qml Frameless

Qt qml Frameless

main.qml (demo)

//main.qml
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3

ApplicationWindow {
    id: mainWindow
    width: 400
    height: 400
    visible: true
    title: qsTr('Frameless')
    color: "lightgray"
    flags: Qt.Window | Qt.FramelessWindowHint

    //custom title
    CustomTitleBar{
        anchors.top: parent.top
        anchors.left: parent.left
        height: 35
    }

    //draggable support
    WindowFramelessItem{
        anchors.fill: parent
    }
}


CustomTitleBar.qml: custom title

//CustomTitleBar.qml
import QtQuick 2.0
import QtQuick.Controls 2.12

//custom title
Item {
    anchors.fill: parent

    property int previousX
    property int previousY

    Rectangle {
        id: titleBar
        width: parent.width
        height: 35
        color: "gray"
        clip: true

        MouseArea {
            anchors.fill: parent

            onPressed: {
                previousX = mouseX
                previousY = mouseY
            }

            onMouseXChanged: {
                var dx = mouseX - previousX
                mainWindow.setX(mainWindow.x + dx)
            }

            onMouseYChanged: {
                var dy = mouseY - previousY
                mainWindow.setY(mainWindow.y + dy)
            }
        }

        //main menu
        MenuBar{
            Menu {
                title: "文件";
                MenuItem{
                    text: "退出";
                    onTriggered: Qt.quit();
                }
            }
            Menu {
                title: "帮助";
                MenuItem{
                    text: "关于";
                    onTriggered: root.showAbout();
                }
                MenuItem{
                    text: "官网";
                    onTriggered: Qt.openUrlExternally("http://www.baidu.com");
                }
            }
        }


        //maxmun, minmun, close
        Button {
            id: closeBtn
            width: titleBar.height
            height: width
            anchors.right: titleBar.right
        }

        Button {
            id: mixmunBtn
            width: titleBar.height
            height: width
            anchors.right: closeBtn.left
            anchors.rightMargin: 1
        }

        Button {
            id: minmunBtn
            width: titleBar.height
            height: width
            anchors.right: mixmunBtn.left
            anchors.rightMargin: 1
        }

    }

}

WindowFramelessItem.qml: draggable support

//WindowFramelessItem.qml
import QtQuick 2.0

//draggable support
Item {
    anchors.fill: parent

    property int previousX
    property int previousY

    MouseArea {
        width: 5

        anchors {
            right: parent.right
            top: parent.top
            bottom: parent.bottom
        }

        cursorShape: Qt.SizeHorCursor

        onPressed: previousX = mouseX

        onMouseXChanged: {
            var dx = mouseX - previousX
            mainWindow.setWidth(parent.width + dx)
        }

    }

    MouseArea {
        height: 5
        anchors {
            top: parent.top
            left: parent.left
            right: parent.right
        }

        cursorShape: Qt.SizeVerCursor

        onPressed: previousY = mouseY

        onMouseYChanged: {
            var dy = mouseY - previousY
            mainWindow.setY(mainWindow.y + dy)
            mainWindow.setHeight(mainWindow.height - dy)

        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值