QML自定义标题栏

import QtQuick 2.9
import QtQuick.Window 2.2

 
Window {
    id: mainWindow
    visible: true
    width: 540
    height: 330
    title: qsTr("Hello World")
    flags: Qt.Window | Qt.FramelessWindowHint
    color:"#262626"
    Rectangle {
        id: title
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        height: 30
        color: "#979797"

 
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton
            property point clickPos: "0,0"
            onPressed: {
                clickPos = Qt.point(mouse.x, mouse.y)
            }

 
            onPositionChanged: {
                //鼠标偏移量
                var delta = Qt.point(mouse.x - clickPos.x, mouse.y - clickPos.y)

 
                //如果mainwindow继承自QWidget,用setPos
                mainWindow.setX(mainWindow.x + delta.x)
                mainWindow.setY(mainWindow.y + delta.y)
            }
        }
    }
}

 

 

你可能感兴趣的:(QT)